1 /* 32-bit ELF support for C-SKY.
2 Copyright (C) 1998-2019 Free Software Foundation, Inc.
3 Contributed by C-SKY Microsystems and Mentor Graphics.
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. */
28 #include "opcode/csky.h"
30 #include "libiberty.h"
32 /* Data structures used for merging different arch variants.
33 V1 (510/610) and V2 (8xx) processors are incompatible, but
34 we can merge wthin each family. */
42 typedef struct csky_arch_for_merge
45 const unsigned long arch_eflag;
46 /* The files can merge only if they are in same class. */
47 enum merge_class class;
48 /* When input files have different levels,
49 the target sets arch_eflag to the largest level file's arch_eflag. */
50 unsigned int class_level;
51 /* Control whether to print warning when merging with different arch. */
52 unsigned int do_warning;
53 } csky_arch_for_merge;
55 static struct csky_arch_for_merge csky_archs[] =
57 /* 510 and 610 merge to 610 without warning. */
58 { "510", CSKY_ARCH_510, CSKY_V1, 0, 0},
59 { "610", CSKY_ARCH_610, CSKY_V1, 1, 0},
60 /* 801, 802, 803, 807, 810 merge to largest one. */
61 { "801", CSKY_ARCH_801, CSKY_V2, 0, 1},
62 { "802", CSKY_ARCH_802, CSKY_V2, 1, 1},
63 { "803", CSKY_ARCH_803, CSKY_V2, 2, 1},
64 { "807", CSKY_ARCH_807, CSKY_V2, 3, 1},
65 { "810", CSKY_ARCH_810, CSKY_V2, 4, 1},
69 /* Return the ARCH bits out of ABFD. */
70 #define bfd_csky_arch(abfd) \
71 (elf_elfheader (abfd)->e_flags & CSKY_ARCH_MASK)
73 /* Return the ABI bits out of ABFD. */
74 #define bfd_csky_abi(abfd) \
75 (elf_elfheader (abfd)->e_flags & CSKY_ABI_MASK)
78 /* The index of a howto-item is implicitly equal to
79 the corresponding Relocation Type Encoding. */
80 static reloc_howto_type csky_elf_howto_table[] =
83 HOWTO (R_CKCORE_NONE, /* type */
87 FALSE, /* pc_relative */
89 complain_overflow_dont, /* complain_on_overflow */
90 NULL, /* special_function */
91 "R_CKCORE_NONE", /* name */
92 FALSE, /* partial_inplace */
95 FALSE), /* pcrel_offset */
98 HOWTO (R_CKCORE_ADDR32, /* type */
102 FALSE, /* pc_relative */
104 complain_overflow_dont, /* complain_on_overflow */
105 bfd_elf_generic_reloc, /* special_function */
106 "R_CKCORE_ADDR32", /* name */
107 FALSE, /* partial_inplace */
109 0xffffffff, /* dst_mask */
110 FALSE), /* pcrel_offset */
112 /* 2: Only for csky v1. */
113 HOWTO (R_CKCORE_PCREL_IMM8BY4, /* type */
117 TRUE, /* pc_relative */
119 complain_overflow_bitfield, /* complain_on_overflow */
120 NULL, /* special_function */
121 "R_CKCORE_PCREL_IMM8BY4", /* name */
122 FALSE, /* partial_inplace */
125 TRUE), /* pcrel_offset */
127 /* 3: Only for csky v1. */
128 HOWTO (R_CKCORE_PCREL_IMM11BY2, /* type */
132 TRUE, /* pc_relative */
134 complain_overflow_signed, /* complain_on_overflow */
135 bfd_elf_generic_reloc, /* special_function */
136 "R_CKCORE_PCREL_IMM11BY2", /* name */
137 FALSE, /* partial_inplace */
138 0x7ff, /* src_mask */
139 0x7ff, /* dst_mask */
140 TRUE), /* pcrel_offset */
143 HOWTO (R_CKCORE_PCREL_IMM4BY2,0,0,0,0,0,0,0,"R_CKCORE_PCREL_IMM4BY2",0,0,0,0),
146 HOWTO (R_CKCORE_PCREL32, /* type */
150 TRUE, /* pc_relative */
152 complain_overflow_dont, /* complain_on_overflow */
153 bfd_elf_generic_reloc, /* special_function */
154 "R_CKCORE_PCREL32", /* name */
155 FALSE, /* partial_inplace */
157 0xffffffff, /* dst_mask */
158 TRUE), /* pcrel_offset */
160 /* 6: Only for csky v1. */
161 HOWTO (R_CKCORE_PCREL_JSR_IMM11BY2, /* type */
165 TRUE, /* pc_relative */
167 complain_overflow_signed, /* complain_on_overflow */
168 bfd_elf_generic_reloc, /* special_function */
169 "R_CKCORE_PCREL_JSR_IMM11BY2", /* name */
170 FALSE, /* partial_inplace */
171 0x7ff, /* src_mask */
172 0x7ff, /* dst_mask */
173 TRUE), /* pcrel_offset */
175 /* 7: GNU extension to record C++ vtable member usage. */
176 HOWTO (R_CKCORE_GNU_VTENTRY, /* type */
180 FALSE, /* pc_relative */
182 complain_overflow_dont, /* complain_on_overflow */
183 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
184 "R_CKCORE_GNU_VTENTRY", /* name */
185 FALSE, /* partial_inplace */
188 FALSE), /* pcrel_offset */
190 /* 8: GNU extension to record C++ vtable hierarchy. */
191 HOWTO (R_CKCORE_GNU_VTINHERIT, /* type */
195 FALSE, /* pc_relative */
197 complain_overflow_dont, /* complain_on_overflow */
198 NULL, /* special_function */
199 "R_CKCORE_GNU_VTINHERIT", /* name */
200 FALSE, /* partial_inplace */
203 FALSE), /* pcrel_offset */
206 HOWTO (R_CKCORE_RELATIVE, /* type */
210 FALSE, /* pc_relative */
212 complain_overflow_signed, /* complain_on_overflow */
213 bfd_elf_generic_reloc, /* special_function */
214 "R_CKCORE_RELATIVE", /* name */
215 TRUE, /* partial_inplace */
217 0xffffffff, /* dst_mask */
218 FALSE), /* pcrel_offset */
221 /* FIXME: It is a bug that copy relocations are not implemented. */
222 HOWTO (R_CKCORE_COPY, /* type */
226 FALSE, /* pc_relative */
228 complain_overflow_bitfield, /* complain_on_overflow */
229 bfd_elf_generic_reloc, /* special_function */
230 "R_CKCORE_COPY", /* name */
231 TRUE, /* partial_inplace */
232 0xffffffff, /* src_mask */
233 0xffffffff, /* dst_mask */
234 FALSE), /* pcrel_offset */
237 HOWTO (R_CKCORE_GLOB_DAT,0,0,0,0,0,0,0,"R_CKCORE_GLOB_DAT",0,0,0,0),
240 HOWTO (R_CKCORE_JUMP_SLOT,0,0,0,0,0,0,0,"R_CKCORE_JUMP_SLOT",0,0,0,0),
243 HOWTO (R_CKCORE_GOTOFF, /* type */
247 FALSE, /* pc_relative */
249 complain_overflow_dont, /* complain_on_overflow */
250 bfd_elf_generic_reloc, /* special_function */
251 "R_CKCORE_GOTOFF", /* name */
252 TRUE, /* partial_inplace */
254 0xffffffffl, /* dst_mask */
255 FALSE), /* pcrel_offset */
258 HOWTO (R_CKCORE_GOTPC, /* type */
262 TRUE, /* pc_relative */
264 complain_overflow_dont, /* complain_on_overflow */
265 bfd_elf_generic_reloc, /* special_function */
266 "R_CKCORE_GOTPC", /* name */
267 TRUE, /* partial_inplace */
269 0xffffffff, /* dst_mask */
270 FALSE), /* pcrel_offset */
273 HOWTO (R_CKCORE_GOT32, /* type */
277 FALSE, /* pc_relative */
279 complain_overflow_dont, /* complain_on_overflow */
280 bfd_elf_generic_reloc, /* special_function */
281 "R_CKCORE_GOT32", /* name */
282 TRUE, /* partial_inplace */
284 0xffffffff, /* dst_mask */
285 TRUE), /* pcrel_offset */
288 HOWTO (R_CKCORE_PLT32, /* type */
292 FALSE, /* pc_relative */
294 complain_overflow_dont, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_CKCORE_PLT32", /* name */
297 TRUE, /* partial_inplace */
299 0xffffffff, /* dst_mask */
300 TRUE), /* pcrel_offset */
303 HOWTO (R_CKCORE_ADDRGOT,0,0,0,0,0,0,0,"R_CKCORE_ADDRGOT",0,0,0,0),
306 HOWTO (R_CKCORE_ADDRPLT,0,0,0,0,0,0,0,"R_CKCORE_ADDRPLT",0,0,0,0),
308 /* 19: Only for csky v2. */
309 HOWTO (R_CKCORE_PCREL_IMM26BY2, /* type */
313 TRUE, /* pc_relative */
315 complain_overflow_signed, /* complain_on_overflow */
316 bfd_elf_generic_reloc, /* special_function */
317 "R_CKCORE_PCREL_IMM26BY2", /* name */
318 FALSE, /* partial_inplace */
320 0x3ffffff, /* dst_mask */
321 TRUE), /* pcrel_offset */
323 /* 20: Only for csky v2. */
324 HOWTO (R_CKCORE_PCREL_IMM16BY2, /* type */
328 TRUE, /* pc_relative */
330 complain_overflow_signed, /* complain_on_overflow */
331 NULL, /* special_function */
332 "R_CKCORE_PCREL_IMM16BY2", /* name */
333 FALSE, /* partial_inplace */
335 0xffff, /* dst_mask */
336 TRUE), /* pcrel_offset */
338 /* 21: Only for csky v2. */
339 HOWTO (R_CKCORE_PCREL_IMM16BY4, /* type */
343 TRUE, /* pc_relative */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 NULL, /* special_function */
347 "R_CKCORE_PCREL_IMM16BY4", /* name */
348 FALSE, /* partial_inplace */
349 0xffff0000, /* src_mask */
350 0xffff, /* dst_mask */
351 TRUE), /* pcrel_offset */
353 /* 22: Only for csky v2. */
354 HOWTO (R_CKCORE_PCREL_IMM10BY2, /* type */
358 TRUE, /* pc_relative */
360 complain_overflow_signed, /* complain_on_overflow */
361 bfd_elf_generic_reloc, /* special_function */
362 "R_CKCORE_PCREL_IMM10BY2", /* name */
363 FALSE, /* partial_inplace */
365 0x3ff, /* dst_mask */
366 TRUE), /* pcrel_offset */
368 /* 23: Only for csky v2. */
369 HOWTO (R_CKCORE_PCREL_IMM10BY4, /* type */
373 TRUE, /* pc_relative */
375 complain_overflow_bitfield, /* complain_on_overflow */
376 NULL, /* special_function */
377 "R_CKCORE_PCREL_IMM10BY4", /* name */
378 FALSE, /* partial_inplace */
380 0x3ff, /* dst_mask */
381 TRUE), /* pcrel_offset */
383 /* 24: Only for csky v2. */
384 HOWTO (R_CKCORE_ADDR_HI16, /* type */
388 FALSE, /* pc_relative */
390 complain_overflow_dont, /* complain_on_overflow */
391 bfd_elf_generic_reloc, /* special_function */
392 "R_CKCORE_ADDR_HI16", /* name */
393 FALSE, /* partial_inplace */
395 0xffff, /* dst_mask */
396 FALSE), /* pcrel_offset */
399 HOWTO (R_CKCORE_ADDR_LO16, /* type */
403 FALSE, /* pc_relative */
405 complain_overflow_dont, /* complain_on_overflow */
406 bfd_elf_generic_reloc, /* special_function */
407 "R_CKCORE_ADDR_LO16", /* name */
408 FALSE, /* partial_inplace */
410 0xffff, /* dst_mask */
411 FALSE), /* pcrel_offset */
414 HOWTO (R_CKCORE_GOTPC_HI16, /* type */
418 TRUE, /* pc_relative */
420 complain_overflow_dont, /* complain_on_overflow */
421 bfd_elf_generic_reloc, /* special_function */
422 "R_CKCORE_GOTPC_HI16", /* name */
423 FALSE, /* partial_inplace */
425 0xffff, /* dst_mask */
426 FALSE), /* pcrel_offset */
429 HOWTO (R_CKCORE_GOTPC_LO16, /* type */
433 TRUE, /* pc_relative */
435 complain_overflow_dont, /* complain_on_overflow */
436 bfd_elf_generic_reloc, /* special_function */
437 "R_CKCORE_GOTPC_LO16", /* name */
438 FALSE, /* partial_inplace */
440 0xffff, /* dst_mask */
441 FALSE), /* pcrel_offset */
444 HOWTO (R_CKCORE_GOTOFF_HI16, /* type */
448 FALSE, /* pc_relative */
450 complain_overflow_dont, /* complain_on_overflow */
451 bfd_elf_generic_reloc, /* special_function */
452 "R_CKCORE_GOTOFF_HI16", /* name */
453 FALSE, /* partial_inplace */
455 0xffff, /* dst_mask */
456 FALSE), /* pcrel_offset */
459 HOWTO (R_CKCORE_GOTOFF_LO16, /* type */
463 FALSE, /* pc_relative */
465 complain_overflow_dont, /* complain_on_overflow */
466 bfd_elf_generic_reloc, /* special_function */
467 "R_CKCORE_GOTOFF_LO16", /* name */
468 FALSE, /* partial_inplace */
470 0xffff, /* dst_mask */
471 FALSE), /* pcrel_offset */
474 HOWTO (R_CKCORE_GOT12, /* type */
478 FALSE, /* pc_relative */
480 complain_overflow_bitfield, /* complain_on_overflow */
481 bfd_elf_generic_reloc, /* special_function */
482 "R_CKCORE_GOT12", /* name */
483 TRUE, /* partial_inplace */
485 0xfff, /* dst_mask */
486 FALSE), /* pcrel_offset */
489 HOWTO (R_CKCORE_GOT_HI16, /* type */
493 FALSE, /* pc_relative */
495 complain_overflow_dont, /* complain_on_overflow */
496 bfd_elf_generic_reloc, /* special_function */
497 "R_CKCORE_GOT_HI16", /* name */
498 TRUE, /* partial_inplace */
500 0xffff, /* dst_mask */
501 FALSE), /* pcrel_offset */
504 HOWTO (R_CKCORE_GOT_LO16, /* type */
508 FALSE, /* pc_relative */
510 complain_overflow_dont, /* complain_on_overflow */
511 bfd_elf_generic_reloc, /* special_function */
512 "R_CKCORE_GOT_LO16", /* name */
513 TRUE, /* partial_inplace */
515 0xffff, /* dst_mask */
516 FALSE), /* pcrel_offset */
519 HOWTO (R_CKCORE_PLT12, /* type */
523 FALSE, /* pc_relative */
525 complain_overflow_bitfield, /* complain_on_overflow */
526 bfd_elf_generic_reloc, /* special_function */
527 "R_CKCORE_PLT12", /* name */
528 TRUE, /* partial_inplace */
530 0xfff, /* dst_mask */
531 FALSE), /* pcrel_offset */
534 HOWTO (R_CKCORE_PLT_HI16, /* type */
538 FALSE, /* pc_relative */
540 complain_overflow_dont, /* complain_on_overflow */
541 bfd_elf_generic_reloc, /* special_function */
542 "R_CKCORE_PLT_HI16", /* name */
543 TRUE, /* partial_inplace */
545 0xffff, /* dst_mask */
546 FALSE), /* pcrel_offset */
549 HOWTO (R_CKCORE_PLT_LO16, /* type */
553 FALSE, /* pc_relative */
555 complain_overflow_dont, /* complain_on_overflow */
556 bfd_elf_generic_reloc, /* special_function */
557 "R_CKCORE_PLT_LO16", /* name */
558 TRUE, /* partial_inplace */
560 0xffff, /* dst_mask */
561 FALSE), /* pcrel_offset */
564 HOWTO (R_CKCORE_ADDRGOT_HI16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
567 HOWTO (R_CKCORE_ADDRGOT_LO16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
570 HOWTO (R_CKCORE_ADDRPLT_HI16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
573 HOWTO (R_CKCORE_ADDRPLT_LO16,0,0,0,0,0,0,0,"R_CKCORE_",0,0,0,0),
576 HOWTO (R_CKCORE_PCREL_JSR_IMM26BY2, /* type */
580 TRUE, /* pc_relative */
582 complain_overflow_signed, /* complain_on_overflow */
583 bfd_elf_generic_reloc, /* special_function */
584 "R_CKCORE_PCREL_JSR_IMM26BY2", /* name */
585 FALSE, /* partial_inplace */
587 0x3ffffff, /* dst_mask */
588 TRUE), /* pcrel_offset */
591 HOWTO (R_CKCORE_TOFFSET_LO16, /* type */
595 FALSE, /* pc_relative */
597 complain_overflow_unsigned, /* complain_on_overflow */
598 NULL, /* special_function */
599 "R_CKCORE_TOFFSET_LO16", /* name */
600 FALSE, /* partial_inplace */
602 0xffff, /* dst_mask */
603 FALSE), /* pcrel_offset */
606 HOWTO (R_CKCORE_DOFFSET_LO16, /* type */
610 FALSE, /* pc_relative */
612 complain_overflow_unsigned, /* complain_on_overflow */
613 NULL, /* special_function */
614 "R_CKCORE_DOFFSET_LO16", /* name */
615 FALSE, /* partial_inplace */
617 0xffff, /* dst_mask */
618 FALSE), /* pcrel_offset */
621 HOWTO (R_CKCORE_PCREL_IMM18BY2, /* type */
625 TRUE, /* pc_relative */
627 complain_overflow_signed, /* complain_on_overflow */
628 NULL, /* special_function */
629 "R_CKCORE_PCREL_IMM18BY2", /* name */
630 FALSE, /* partial_inplace */
632 0x3ffff, /* dst_mask */
633 TRUE), /* pcrel_offset */
636 HOWTO (R_CKCORE_DOFFSET_IMM18, /* type */
640 FALSE, /* pc_relative */
642 complain_overflow_unsigned, /* complain_on_overflow */
643 NULL, /* special_function */
644 "R_CKCORE_DOFFSET_IMM18", /* name */
645 FALSE, /* partial_inplace */
647 0x3ffff, /* dst_mask */
648 FALSE), /* pcrel_offset */
651 HOWTO (R_CKCORE_DOFFSET_IMM18BY2, /* type */
655 FALSE, /* pc_relative */
657 complain_overflow_unsigned, /* complain_on_overflow */
658 NULL, /* special_function */
659 "R_CKCORE_DOFFSET_IMM18BY2", /* name */
660 FALSE, /* partial_inplace */
662 0x3ffff, /* dst_mask */
663 FALSE), /* pcrel_offset */
666 HOWTO (R_CKCORE_DOFFSET_IMM18BY4, /* type */
670 FALSE, /* pc_relative */
672 complain_overflow_unsigned, /* complain_on_overflow */
673 NULL, /* special_function */
674 "R_CKCORE_DOFFSET_IMM18BY4", /* name */
675 FALSE, /* partial_inplace */
677 0x3ffff, /* dst_mask */
678 FALSE), /* pcrel_offset */
681 HOWTO (R_CKCORE_GOTOFF_IMM18, /* type */
685 FALSE, /* pc_relative */
687 complain_overflow_bitfield, /* complain_on_overflow */
688 bfd_elf_generic_reloc, /* special_function */
689 "R_CKCORE_GOTOFF_IMM18", /* name */
690 TRUE, /* partial_inplace */
691 0xfffc, /* src_mask */
692 0x3ffff, /* dst_mask */
693 FALSE), /* pcrel_offset */
696 HOWTO (R_CKCORE_GOT_IMM18BY4, /* type */
700 FALSE, /* pc_relative */
702 complain_overflow_bitfield, /* complain_on_overflow */
703 bfd_elf_generic_reloc, /* special_function */
704 "R_CKCORE_GOT_IMM18BY4", /* name */
705 TRUE, /* partial_inplace */
706 0xfffc, /* src_mask */
707 0x3ffff, /* dst_mask */
708 FALSE), /* pcrel_offset */
711 HOWTO (R_CKCORE_PLT_IMM18BY4, /* type */
715 FALSE, /* pc_relative */
717 complain_overflow_bitfield, /* complain_on_overflow */
718 bfd_elf_generic_reloc, /* special_function */
719 "R_CKCORE_PLT_IMM18BY4", /* name */
720 TRUE, /* partial_inplace */
721 0xfffc, /* src_mask */
722 0x3ffff, /* dst_mask */
723 TRUE), /* pcrel_offset */
726 HOWTO (R_CKCORE_PCREL_IMM7BY4, /* type */
730 TRUE, /* pc_relative */
732 complain_overflow_bitfield, /* complain_on_overflow */
733 bfd_elf_generic_reloc, /* special_function */
734 "R_CKCORE_PCREL_IMM7BY4", /* name */
735 FALSE, /* partial_inplace */
736 0xec1f, /* src_mask */
737 0x31f, /* dst_mask */
738 TRUE), /* pcrel_offset */
740 /* 51: for static nptl. */
741 HOWTO (R_CKCORE_TLS_LE32, /* type */
745 FALSE, /* pc_relative */
747 complain_overflow_dont, /* complain_on_overflow */
748 bfd_elf_generic_reloc, /* special_function */
749 "R_CKCORE_TLS_LE32", /* name */
750 FALSE, /* partial_inplace */
752 0xffffffff, /* dst_mask */
753 TRUE), /* pcrel_offset */
755 /* 52: for static nptl. */
756 HOWTO (R_CKCORE_TLS_IE32, /* type */
760 FALSE, /* pc_relative */
762 complain_overflow_dont, /* complain_on_overflow */
763 bfd_elf_generic_reloc, /* special_function */
764 "R_CKCORE_TLS_IE32", /* name */
765 FALSE, /* partial_inplace */
767 0xffffffff, /* dst_mask */
768 TRUE), /* pcrel_offset */
770 /* 53: for pic nptl. */
771 HOWTO (R_CKCORE_TLS_GD32, /* type */
775 FALSE, /* pc_relative */
777 complain_overflow_dont, /* complain_on_overflow */
778 bfd_elf_generic_reloc, /* special_function */
779 "R_CKCORE_TLS_GD32", /* name */
780 FALSE, /* partial_inplace */
782 0xffffffff, /* dst_mask */
783 TRUE), /* pcrel_offset */
785 /* 54: for pic nptl. */
786 HOWTO (R_CKCORE_TLS_LDM32, /* type */
790 FALSE, /* pc_relative */
792 complain_overflow_dont, /* complain_on_overflow */
793 bfd_elf_generic_reloc, /* special_function */
794 "R_CKCORE_TLS_LDM32", /* name */
795 FALSE, /* partial_inplace */
797 0xffffffff, /* dst_mask */
798 TRUE), /* pcrel_offset */
800 /* 55: for pic nptl. */
801 HOWTO (R_CKCORE_TLS_LDO32, /* type */
805 FALSE, /* pc_relative */
807 complain_overflow_dont, /* complain_on_overflow */
808 bfd_elf_generic_reloc, /* special_function */
809 "R_CKCORE_TLS_LDO32", /* name */
810 FALSE, /* partial_inplace */
812 0xffffffff, /* dst_mask */
813 TRUE), /* pcrel_offset */
815 /* 56: for linker. */
816 HOWTO (R_CKCORE_TLS_DTPMOD32,0,0,0,0,0,0,0,"R_CKCORE_TLS_DTPMOD32",0,0,0,0),
818 /* 57: for linker. */
819 HOWTO (R_CKCORE_TLS_DTPOFF32,0,0,0,0,0,0,0,"R_CKCORE_TLS_DTPOFF32",0,0,0,0),
821 /* 58: for linker. */
822 HOWTO (R_CKCORE_TLS_TPOFF32,0,0,0,0,0,0,0,"R_CKCORE_TLS_TPOFF32",0,0,0,0),
824 /* 59: for ck807f. */
825 HOWTO (R_CKCORE_PCREL_FLRW_IMM8BY4, /* type */
829 TRUE, /* pc_relative */
831 complain_overflow_bitfield, /* complain_on_overflow */
832 NULL, /* special_function */
833 "R_CKCORE_PCREL_FLRW_IMM8BY4", /* name */
834 FALSE, /* partial_inplace */
835 0xfe1fff0f, /* src_mask */
836 0x1e000f0, /* dst_mask */
837 TRUE), /* pcrel_offset */
839 /* 60: for 810 not to generate jsri. */
840 HOWTO (R_CKCORE_NOJSRI, /* type */
844 FALSE, /* pc_relative */
846 complain_overflow_dont, /* complain_on_overflow */
847 bfd_elf_generic_reloc, /* special_function */
848 "R_CKCORE_NOJSRI", /* name */
849 FALSE, /* partial_inplace */
850 0xffff, /* src_mask */
851 0xffff, /* dst_mask */
852 FALSE), /* pcrel_offset */
854 /* 61: for callgraph. */
855 HOWTO (R_CKCORE_CALLGRAPH, /* type */
859 FALSE, /* pc_relative */
861 complain_overflow_dont, /* complain_on_overflow */
862 NULL, /* special_function */
863 "R_CKCORE_CALLGRAPH", /* name */
864 FALSE, /* partial_inplace */
867 TRUE), /* pcrel_offset */
870 HOWTO (R_CKCORE_IRELATIVE,0,0,0,0,0,0,0,"R_CKCORE_IRELATIVE",0,0,0,0),
872 /* 63: for bloop instruction */
873 HOWTO (R_CKCORE_PCREL_BLOOP_IMM4BY4, /* type */
879 complain_overflow_signed, /* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_CKCORE_PCREL_BLOOP_IMM4BY4", /* name */
882 FALSE, /* partial_inplace */
885 TRUE), /* pcrel_offset */
886 /* 64: for bloop instruction */
887 HOWTO (R_CKCORE_PCREL_BLOOP_IMM12BY4, /* type */
893 complain_overflow_signed, /* complain_on_overflow */
894 bfd_elf_generic_reloc, /* special_function */
895 "R_CKCORE_PCREL_BLOOP_IMM12BY4", /* name */
896 FALSE, /* partial_inplace */
898 0xfff, /* dst_mask */
899 TRUE), /* pcrel_offset */
905 /* Whether GOT overflow checking is needed. */
906 static int check_got_overflow = 0;
908 /* Whether the target 32 bits is forced so that the high
909 16 bits is at the low address. */
910 static int need_reverse_bits;
912 /* Used for relaxation. See csky_relocate_contents. */
913 static bfd_vma read_content_substitute;
916 The way the following two look-up functions work demands
917 that BFD_RELOC_CKCORE_xxx are defined contiguously. */
919 static reloc_howto_type *
920 csky_elf_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
921 bfd_reloc_code_real_type code)
923 int csky_code = code - BFD_RELOC_CKCORE_NONE;
925 if (csky_code < 0 || csky_code >= R_CKCORE_MAX)
930 csky_code = R_CKCORE_NONE;
933 csky_code = R_CKCORE_ADDR32;
935 case BFD_RELOC_32_PCREL:
936 csky_code = R_CKCORE_PCREL32;
938 case BFD_RELOC_VTABLE_INHERIT:
939 csky_code = R_CKCORE_GNU_VTINHERIT;
941 case BFD_RELOC_VTABLE_ENTRY:
942 csky_code = R_CKCORE_GNU_VTENTRY;
945 csky_code = R_CKCORE_RELATIVE;
948 return (reloc_howto_type *)NULL;
951 /* Note: when adding csky bfd reloc types in bfd-in2.h
952 and csky elf reloc types in elf/csky.h,
953 the order of the two reloc type tables should be consistent. */
954 return &csky_elf_howto_table[csky_code];
957 static reloc_howto_type *
958 csky_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
962 for (i = 0; i < R_CKCORE_MAX; i++)
963 if (strcasecmp (csky_elf_howto_table[i].name, r_name) == 0)
964 return &csky_elf_howto_table[i];
968 static reloc_howto_type *
969 elf32_csky_howto_from_type (unsigned int r_type)
971 if (r_type < R_CKCORE_MAX)
972 return &csky_elf_howto_table[r_type];
978 csky_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
980 Elf_Internal_Rela *dst)
984 r_type = ELF32_R_TYPE (dst->r_info);
985 cache_ptr->howto = elf32_csky_howto_from_type (r_type);
986 if (cache_ptr->howto == NULL)
988 /* xgettext:c-format */
989 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
991 bfd_set_error (bfd_error_bad_value);
997 /* The Global Offset Table max size. */
998 #define GOT_MAX_SIZE 0xFFFF8
1000 /* The name of the dynamic interpreter. This is put in the .interp
1002 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
1004 /* The size in bytes of an entry in the procedure linkage table. */
1005 #define PLT_ENTRY_SIZE 12
1006 #define PLT_ENTRY_SIZE_P 16
1008 /* The first entry in a procedure linkage table looks like
1009 this. It is set up so that any shared library function that is
1010 called before the relocation has been set up calls the dynamic
1012 static const bfd_vma csky_elf_plt_entry_v2[PLT_ENTRY_SIZE / 4] =
1014 0xd99c2002, /* ldw r12, (gb, 8) */
1015 0xea0d0000, /* movi r13,offset */
1016 0xe8cc0000 /* jmp r12 */
1019 static const bfd_vma csky_elf_plt_entry_v1[PLT_ENTRY_SIZE / 2 ] =
1021 0x25f0, /* subi r0, 32 */
1022 0x9200, /* stw r2, (r0, 0) */
1023 0x9310, /* stw r3, (r0, 4) */
1024 0x822e, /* ldw r2, (gb, 8) */
1025 0x7301, /* lrw r3, #offset */
1026 0x00c2, /* jmp r2 */
1029 /* Branch stub support. */
1038 bfd_boolean use_branch_stub = TRUE;
1042 enum stub_insn_type type;
1043 unsigned int r_type;
1047 static const insn_sequence elf32_csky_stub_long_branch[] =
1049 {0xea8d0002, INSN32, R_CKCORE_NONE, 0x0}, /* lrw t1,[pc+8] */
1050 {0x7834, INSN16, R_CKCORE_NONE, 0x0}, /* jmp t1 */
1051 {0x6c03, INSN16, R_CKCORE_NONE, 0x0}, /* nop */
1052 {0x0, DATA_TYPE, R_CKCORE_ADDR32, 0x0} /* .long addr */
1055 static const insn_sequence elf32_csky_stub_long_branch_jmpi[] =
1057 {0xeac00001, INSN32, R_CKCORE_NONE, 0x0}, /* jmpi [pc+4] */
1058 {0x0, DATA_TYPE, R_CKCORE_ADDR32, 0x0} /* .long addr */
1061 /* The bsr instruction offset limit. */
1062 #define BSR_MAX_FWD_BRANCH_OFFSET (((1 << 25) - 1) << 1)
1063 #define BSR_MAX_BWD_BRANCH_OFFSET (-(1 << 26))
1065 #define STUB_SUFFIX ".stub"
1066 #define STUB_ENTRY_NAME "__%s_veneer"
1068 /* One entry per long/short branch stub defined above. */
1070 DEF_STUB(long_branch) \
1071 DEF_STUB(long_branch_jmpi)
1073 #define DEF_STUB(x) csky_stub_##x,
1074 enum elf32_csky_stub_type
1083 const insn_sequence* template_sequence;
1087 #define DEF_STUB(x) {elf32_csky_stub_##x, ARRAY_SIZE(elf32_csky_stub_##x)},
1088 static const stub_def stub_definitions[] = {
1093 /* The size of the thread control block. */
1096 struct csky_elf_obj_tdata
1098 struct elf_obj_tdata root;
1100 /* tls_type for each local got entry. */
1101 char *local_got_tls_type;
1104 #define csky_elf_local_got_tls_type(bfd) \
1105 (csky_elf_tdata (bfd)->local_got_tls_type)
1107 #define csky_elf_tdata(bfd) \
1108 ((struct csky_elf_obj_tdata *) (bfd)->tdata.any)
1110 struct elf32_csky_stub_hash_entry
1112 /* Base hash table entry structure. */
1113 struct bfd_hash_entry root;
1115 /* The stub section. */
1118 /* Offset within stub_sec of the beginning of this stub. */
1119 bfd_vma stub_offset;
1121 /* Given the symbol's value and its section we can determine its final
1122 value when building the stubs (so the stub knows where to jump). */
1123 bfd_vma target_value;
1124 asection *target_section;
1126 /* Offset to apply to relocation referencing target_value. */
1127 bfd_vma target_addend;
1129 /* The stub type. */
1130 enum elf32_csky_stub_type stub_type;
1131 /* Its encoding size in bytes. */
1134 const insn_sequence *stub_template;
1135 /* The size of the template (number of entries). */
1136 int stub_template_size;
1138 /* The symbol table entry, if any, that this was derived from. */
1139 struct csky_elf_link_hash_entry *h;
1141 /* Destination symbol type. */
1142 unsigned char st_type;
1144 /* Where this stub is being called from, or, in the case of combined
1145 stub sections, the first input section in the group. */
1148 /* The name for the local symbol at the start of this stub. The
1149 stub name in the hash table has to be unique; this does not, so
1150 it can be friendlier. */
1154 #define csky_stub_hash_lookup(table, string, create, copy) \
1155 ((struct elf32_csky_stub_hash_entry *) \
1156 bfd_hash_lookup ((table), (string), (create), (copy)))
1158 /* C-SKY ELF linker hash entry. */
1159 struct csky_elf_link_hash_entry
1161 struct elf_link_hash_entry elf;
1163 /* For sub jsri2bsr relocs count. */
1164 int jsri2bsr_refcount;
1165 /* Track dynamic relocs copied for this symbol. */
1166 struct elf_dyn_relocs *dyn_relocs;
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
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 \
1184 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
1187 /* Get the C-SKY ELF linker hash table from a link_info structure. */
1188 #define csky_elf_hash_table(info) \
1189 ((elf_hash_table_id ((struct elf_link_hash_table *) ((info)->hash)) \
1191 ? ((struct csky_elf_link_hash_table *) ((info)->hash)) \
1194 #define csky_elf_hash_entry(ent) ((struct csky_elf_link_hash_entry*)(ent))
1196 /* Array to keep track of which stub sections have been created, and
1197 information on stub grouping. */
1200 /* This is the section to which stubs in the group will be
1203 /* The stub section. */
1207 /* C-SKY ELF linker hash table. */
1208 struct csky_elf_link_hash_table
1210 struct elf_link_hash_table elf;
1212 /* Small local sym cache. */
1213 struct sym_cache sym_cache;
1215 /* Data for R_CKCORE_TLS_LDM32 relocations. */
1218 bfd_signed_vma refcount;
1222 /* The stub hash table. */
1223 struct bfd_hash_table stub_hash_table;
1225 /* Linker stub bfd. */
1228 /* Linker call-backs. */
1229 asection * (*add_stub_section) (const char *, asection *);
1230 void (*layout_sections_again) (void);
1232 /* Array to keep track of which stub sections have been created, and
1233 * information on stub grouping. */
1234 struct map_stub *stub_group;
1236 /* Number of elements in stub_group. */
1237 unsigned int top_id;
1239 /* Assorted information used by elf32_csky_size_stubs. */
1240 unsigned int bfd_count;
1241 unsigned int top_index;
1242 asection **input_list;
1245 /* We can't change vectors in the bfd target which will apply to
1246 data sections, however we only do this to the text sections. */
1249 csky_get_insn_32 (bfd *input_bfd,
1252 if (bfd_big_endian (input_bfd))
1253 return bfd_get_32 (input_bfd, location);
1255 return (bfd_get_16 (input_bfd, location) << 16
1256 | bfd_get_16 (input_bfd, location + 2));
1260 csky_put_insn_32 (bfd *input_bfd,
1264 if (bfd_big_endian (input_bfd))
1265 bfd_put_32 (input_bfd, x, location);
1268 bfd_put_16 (input_bfd, x >> 16, location);
1269 bfd_put_16 (input_bfd, x & 0xffff, location + 2);
1273 /* Find or create a stub section. Returns a pointer to the stub section, and
1274 the section to which the stub section will be attached (in *LINK_SEC_P).
1275 LINK_SEC_P may be NULL. */
1278 elf32_csky_create_or_find_stub_sec (asection **link_sec_p, asection *section,
1279 struct csky_elf_link_hash_table *htab)
1284 link_sec = htab->stub_group[section->id].link_sec;
1285 stub_sec = htab->stub_group[section->id].stub_sec;
1286 if (stub_sec == NULL)
1288 stub_sec = htab->stub_group[link_sec->id].stub_sec;
1289 if (stub_sec == NULL)
1295 namelen = strlen (link_sec->name);
1296 len = namelen + sizeof (STUB_SUFFIX);
1297 s_name = bfd_alloc (htab->stub_bfd, len);
1301 memcpy (s_name, link_sec->name, namelen);
1302 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
1303 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
1304 if (stub_sec == NULL)
1306 htab->stub_group[link_sec->id].stub_sec = stub_sec;
1308 htab->stub_group[section->id].stub_sec = stub_sec;
1312 *link_sec_p = link_sec;
1317 /* Build a name for an entry in the stub hash table. */
1320 elf32_csky_stub_name (const asection *input_section,
1321 const asection *sym_sec,
1322 const struct csky_elf_link_hash_entry *hash,
1323 const Elf_Internal_Rela *rel)
1330 len = 8 + 1 + strlen (hash->elf.root.root.string) + 1 + 8 + 1;
1331 stub_name = bfd_malloc (len);
1332 if (stub_name != NULL)
1333 sprintf (stub_name, "%08x_%s+%x",
1334 input_section->id & 0xffffffff,
1335 hash->elf.root.root.string,
1336 (int) rel->r_addend & 0xffffffff);
1340 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
1341 stub_name = bfd_malloc (len);
1342 if (stub_name != NULL)
1343 sprintf (stub_name, "%08x_%x:%x+%x",
1344 input_section->id & 0xffffffff,
1345 sym_sec->id & 0xffffffff,
1346 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff,
1347 (int) rel->r_addend & 0xffffffff);
1353 /* Determine the type of stub needed, if any, for a call. */
1355 static enum elf32_csky_stub_type
1356 csky_type_of_stub (struct bfd_link_info *info,
1357 asection *input_sec,
1358 const Elf_Internal_Rela *rel,
1359 unsigned char st_type,
1360 struct csky_elf_link_hash_entry *hash,
1361 bfd_vma destination,
1362 asection *sym_sec ATTRIBUTE_UNUSED,
1363 bfd *input_bfd ATTRIBUTE_UNUSED,
1364 const char *name ATTRIBUTE_UNUSED)
1367 bfd_signed_vma branch_offset;
1368 unsigned int r_type;
1369 enum elf32_csky_stub_type stub_type = csky_stub_none;
1370 struct elf_link_hash_entry * h = &hash->elf;
1372 /* We don't know the actual type of destination in case it is of
1373 type STT_SECTION: give up. */
1374 if (st_type == STT_SECTION)
1377 location = (input_sec->output_offset
1378 + input_sec->output_section->vma
1381 branch_offset = (bfd_signed_vma)(destination - location);
1382 r_type = ELF32_R_TYPE (rel->r_info);
1383 if (r_type == R_CKCORE_PCREL_IMM26BY2
1385 && ((h->def_dynamic && !h->def_regular)
1386 || (bfd_link_pic (info)
1387 && h->root.type == bfd_link_hash_defweak)))
1388 || branch_offset > BSR_MAX_FWD_BRANCH_OFFSET
1389 || branch_offset < BSR_MAX_BWD_BRANCH_OFFSET))
1391 if (bfd_csky_arch (info->output_bfd) == CSKY_ARCH_810
1392 || bfd_csky_arch (info->output_bfd) == CSKY_ARCH_807)
1393 stub_type = csky_stub_long_branch_jmpi;
1395 stub_type = csky_stub_long_branch;
1401 /* Create an entry in an C-SKY ELF linker hash table. */
1403 static struct bfd_hash_entry *
1404 csky_elf_link_hash_newfunc (struct bfd_hash_entry * entry,
1405 struct bfd_hash_table * table,
1406 const char * string)
1408 struct csky_elf_link_hash_entry * ret =
1409 (struct csky_elf_link_hash_entry *) entry;
1411 /* Allocate the structure if it has not already been allocated by a
1415 ret = (struct csky_elf_link_hash_entry *)
1416 bfd_hash_allocate (table,
1417 sizeof (struct csky_elf_link_hash_entry));
1419 return (struct bfd_hash_entry *) ret;
1422 /* Call the allocation method of the superclass. */
1423 ret = ((struct csky_elf_link_hash_entry *)
1424 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *)ret,
1428 struct csky_elf_link_hash_entry *eh;
1430 eh = (struct csky_elf_link_hash_entry *) ret;
1431 eh->dyn_relocs = NULL;
1432 eh->plt_refcount = 0;
1433 eh->jsri2bsr_refcount = 0;
1434 eh->tls_type = GOT_NORMAL;
1435 ret->stub_cache = NULL;
1438 return (struct bfd_hash_entry *) ret;
1441 /* Initialize an entry in the stub hash table. */
1443 static struct bfd_hash_entry *
1444 stub_hash_newfunc (struct bfd_hash_entry *entry,
1445 struct bfd_hash_table *table,
1448 /* Allocate the structure if it has not already been allocated by a
1452 entry = ((struct bfd_hash_entry *)
1453 bfd_hash_allocate (table,
1454 sizeof (struct elf32_csky_stub_hash_entry)));
1459 /* Call the allocation method of the superclass. */
1460 entry = bfd_hash_newfunc (entry, table, string);
1463 struct elf32_csky_stub_hash_entry *eh;
1465 /* Initialize the local fields. */
1466 eh = (struct elf32_csky_stub_hash_entry *) entry;
1467 eh->stub_sec = NULL;
1468 eh->stub_offset = 0;
1469 eh->target_value = 0;
1470 eh->target_section = NULL;
1471 eh->target_addend = 0;
1472 eh->stub_type = csky_stub_none;
1474 eh->stub_template = NULL;
1475 eh->stub_template_size = -1;
1478 eh->output_name = NULL;
1484 /* Free the derived linker hash table. */
1487 csky_elf_link_hash_table_free (bfd *obfd)
1489 struct csky_elf_link_hash_table *ret
1490 = (struct csky_elf_link_hash_table *) obfd->link.hash;
1492 bfd_hash_table_free (&ret->stub_hash_table);
1493 _bfd_elf_link_hash_table_free (obfd);
1496 /* Create an CSKY elf linker hash table. */
1498 static struct bfd_link_hash_table *
1499 csky_elf_link_hash_table_create (bfd *abfd)
1501 struct csky_elf_link_hash_table *ret;
1502 bfd_size_type amt = sizeof (struct csky_elf_link_hash_table);
1504 ret = (struct csky_elf_link_hash_table*) bfd_zmalloc (amt);
1508 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1509 csky_elf_link_hash_newfunc,
1510 sizeof (struct csky_elf_link_hash_entry),
1517 if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
1518 sizeof (struct elf32_csky_stub_hash_entry)))
1523 ret->elf.root.hash_table_free = csky_elf_link_hash_table_free;
1524 return &ret->elf.root;
1528 csky_elf_mkobject (bfd *abfd)
1530 return bfd_elf_allocate_object (abfd, sizeof (struct csky_elf_obj_tdata),
1534 /* Adjust a symbol defined by a dynamic object and referenced by a
1535 regular object. The current definition is in some section of the
1536 dynamic object, but we're not including those sections. We have to
1537 change the definition to something the rest of the link can
1541 csky_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
1542 struct elf_link_hash_entry *h)
1544 struct csky_elf_link_hash_entry *eh;
1545 struct csky_elf_link_hash_table *htab;
1548 eh = (struct csky_elf_link_hash_entry *)h;
1552 htab = csky_elf_hash_table (info);
1556 /* Clear jsri2bsr_refcount, if creating shared library files. */
1557 if (bfd_link_pic (info) && eh->jsri2bsr_refcount > 0)
1558 eh->jsri2bsr_refcount = 0;
1560 /* If there is a function, put it in the procedure linkage table. We
1561 will fill in the contents of the procedure linkage table later. */
1564 /* Calls to STT_GNU_IFUNC symbols always use a PLT, even if the
1565 symbol binds locally. */
1566 if (h->plt.refcount <= 0
1567 || (h->type != STT_GNU_IFUNC
1568 && (SYMBOL_CALLS_LOCAL (info, h)
1569 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1570 && h->root.type == bfd_link_hash_undefweak))))
1573 /* This case can occur if we saw a PLT32 reloc in an input
1574 file, but the symbol was never referred to by a dynamic
1575 object, or if all references were garbage collected. In
1576 such a case, we don't actually need to build a procedure
1577 linkage table, and we can just do a PC32 reloc instead. */
1578 h->plt.offset = (bfd_vma) -1;
1580 if (h->got.refcount == 0)
1581 h->got.refcount += 1;
1583 else if (h->got.refcount != 0)
1585 h->got.refcount -= eh->plt_refcount;
1586 eh->plt_refcount = 0;
1591 /* It's possible that we incorrectly decided a .plt reloc was
1592 needed for an R_CKCORE_PC32 or similar reloc to a non-function
1593 sym in check_relocs. We can't decide accurately between function
1594 and non-function syms in check_relocs; objects loaded later in
1595 the link may change h->type. So fix it now. */
1596 h->plt.offset = (bfd_vma) -1;
1598 /* If this is a weak symbol, and there is a real definition, the
1599 processor independent code will have arranged for us to see the
1600 real definition first, and we can just use the same value. */
1601 if (h->is_weakalias)
1603 struct elf_link_hash_entry *def = weakdef (h);
1604 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1605 h->root.u.def.section = def->root.u.def.section;
1606 h->root.u.def.value = def->root.u.def.value;
1610 /* If there are no non-GOT references, we do not need a copy
1612 if (!h->non_got_ref)
1615 /* This is a reference to a symbol defined by a dynamic object which
1616 is not a function. */
1618 /* If we are creating a shared library, we must presume that the
1619 only references to the symbol are via the global offset table.
1620 For such cases we need not do anything here; the relocations will
1621 be handled correctly by relocate_section. */
1622 if (bfd_link_pic (info) || htab->elf.is_relocatable_executable)
1625 /* We must allocate the symbol in our .dynbss section, which will
1626 become part of the .bss section of the executable. There will be
1627 an entry for this symbol in the .dynsym section. The dynamic
1628 object will contain position independent code, so all references
1629 from the dynamic object to this symbol will go through the global
1630 offset table. The dynamic linker will use the .dynsym entry to
1631 determine the address it must put in the global offset table, so
1632 both the dynamic object and the regular object will refer to the
1633 same memory location for the variable. */
1634 /* We must generate a R_CKCORE_COPY reloc to tell the dynamic linker to
1635 copy the initial value out of the dynamic object and into the
1636 runtime process image. We need to remember the offset into the
1637 .rela.bss section we are going to use. */
1638 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
1640 s = htab->elf.sdynrelro;
1641 srel = htab->elf.sreldynrelro;
1645 s = htab->elf.sdynbss;
1646 srel = htab->elf.srelbss;
1648 if (info->nocopyreloc == 0
1649 && (h->root.u.def.section->flags & SEC_ALLOC) != 0
1654 srel->size += sizeof (Elf32_External_Rela);
1656 return _bfd_elf_adjust_dynamic_copy (info, h, s);
1663 /* Allocate space in .plt, .got and associated reloc sections for
1667 csky_allocate_dynrelocs (struct elf_link_hash_entry *h, PTR inf)
1669 struct bfd_link_info *info;
1670 struct csky_elf_link_hash_table *htab;
1671 struct csky_elf_link_hash_entry *eh;
1672 struct elf_dyn_relocs *p;
1674 /* For indirect case, such as _ZdlPv to _ZdlPv@@GLIBCXX_3.4. */
1675 if (h->root.type == bfd_link_hash_indirect)
1678 if (h->root.type == bfd_link_hash_warning)
1679 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1682 info = (struct bfd_link_info *) inf;
1683 htab = csky_elf_hash_table (info);
1686 /*TODO: how to deal with weak symbol relocs. */
1687 if ((htab->elf.dynamic_sections_created || h->type == STT_GNU_IFUNC)
1688 && h->plt.refcount > 0)
1690 /* Make sure this symbol is output as a dynamic symbol.
1691 Undefined weak syms won't yet be marked as dynamic. */
1692 if (h->dynindx == -1 && !h->forced_local
1693 && h->root.type == bfd_link_hash_undefweak
1694 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1696 if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1698 asection *splt = htab->elf.splt;
1700 /* If this is the first .plt entry, make room for the special
1702 if (splt->size == 0)
1704 if (bfd_csky_abi (info->output_bfd) == CSKY_ABI_V1)
1705 splt->size += PLT_ENTRY_SIZE_P;
1707 splt->size += PLT_ENTRY_SIZE;
1709 h->plt.offset = splt->size;
1711 /* If this symbol is not defined in a regular file, and we are
1712 not generating a shared library, then set the symbol to this
1713 location in the .plt. This is required to make function
1714 pointers compare as equal between the normal executable and
1715 the shared library. */
1716 if (!bfd_link_pic (info) && !h->def_regular)
1718 h->root.u.def.section = splt;
1719 h->root.u.def.value = h->plt.offset;
1722 /* Make room for this entry. */
1723 if (bfd_csky_abi (info->output_bfd) == CSKY_ABI_V1)
1724 splt->size += PLT_ENTRY_SIZE_P;
1726 splt->size += PLT_ENTRY_SIZE;
1727 /* We also need to make an entry in the .rela.plt section. */
1728 htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
1730 /* We also need to make an entry in the .got.plt section, which
1731 will be placed in the .got section by the linker script. */
1732 htab->elf.sgotplt->size += 4;
1736 h->plt.offset = (bfd_vma) -1;
1742 h->plt.offset = (bfd_vma) -1;
1746 if (h->got.refcount > 0)
1752 int tls_type = csky_elf_hash_entry (h)->tls_type;
1753 /* Make sure this symbol is output as a dynamic symbol.
1754 Undefined weak syms won't yet be marked as dynamic. */
1755 if (h->dynindx == -1 && !h->forced_local
1756 && h->root.type == bfd_link_hash_undefweak
1757 && ! bfd_elf_link_record_dynamic_symbol (info, h))
1760 sgot = htab->elf.sgot;
1761 h->got.offset = sgot->size;
1762 BFD_ASSERT (tls_type != GOT_UNKNOWN);
1763 if (tls_type == GOT_NORMAL)
1764 /* Non-TLS symbols need one GOT slot. */
1768 if (tls_type & GOT_TLS_GD)
1769 /* R_CKCORE_TLS_GD32 needs 2 consecutive GOT slots. */
1771 if (tls_type & GOT_TLS_IE)
1772 /* R_CKCORE_TLS_IE32 needs one GOT slot. */
1775 dyn = htab->elf.dynamic_sections_created;
1777 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
1778 && (! bfd_link_pic (info) || !SYMBOL_REFERENCES_LOCAL (info, h)))
1781 if (tls_type != GOT_NORMAL
1782 && (bfd_link_pic (info) || indx != 0)
1783 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1784 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1785 || h->root.type != bfd_link_hash_undefweak))
1787 if (tls_type & GOT_TLS_IE)
1788 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1789 if (tls_type & GOT_TLS_GD)
1790 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1791 if ((tls_type & GOT_TLS_GD) && indx != 0)
1792 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1794 else if (((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1795 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1796 || h->root.type != bfd_link_hash_undefweak)
1797 && (bfd_link_pic (info)
1798 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)
1799 || h->plt.offset == (bfd_vma) -1))
1800 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
1803 h->got.offset = (bfd_vma) -1;
1805 eh = (struct csky_elf_link_hash_entry *) h;
1806 if (eh->dyn_relocs == NULL)
1809 /* In the shared -Bsymbolic case, discard space allocated for
1810 dynamic pc-relative relocs against symbols which turn out to be
1811 defined in regular objects. For the normal shared case, discard
1812 space for pc-relative relocs that have become local due to symbol
1813 visibility changes. */
1815 if (bfd_link_pic (info))
1817 if (SYMBOL_CALLS_LOCAL (info, h))
1819 struct elf_dyn_relocs **pp;
1821 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1823 p->count -= p->pc_count;
1832 if (eh->jsri2bsr_refcount
1833 && h->root.type == bfd_link_hash_defined
1834 && eh->dyn_relocs != NULL)
1835 eh->dyn_relocs->count -= eh->jsri2bsr_refcount;
1837 /* Also discard relocs on undefined weak syms with non-default
1839 if (eh->dyn_relocs != NULL
1840 && h->root.type == bfd_link_hash_undefweak)
1842 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1843 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
1844 eh->dyn_relocs = NULL;
1846 /* Make sure undefined weak symbols are output as a dynamic
1848 else if (h->dynindx == -1
1850 && !bfd_elf_link_record_dynamic_symbol (info, h))
1857 /* For the non-shared case, discard space for relocs against
1858 symbols which turn out to need copy relocs or are not
1862 && ((h->def_dynamic && !h->def_regular)
1863 || (htab->elf.dynamic_sections_created
1864 && (h->root.type == bfd_link_hash_undefweak
1865 || h->root.type == bfd_link_hash_indirect
1866 || h->root.type == bfd_link_hash_undefined))))
1868 /* Make sure this symbol is output as a dynamic symbol.
1869 Undefined weak syms won't yet be marked as dynamic. */
1870 if (h->dynindx == -1 && !h->forced_local
1871 && h->root.type == bfd_link_hash_undefweak)
1873 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1877 /* If that succeeded, we know we'll be keeping all the
1879 if (h->dynindx != -1)
1883 eh->dyn_relocs = NULL;
1888 /* Finally, allocate space. */
1889 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1891 asection *srelgot = htab->elf.srelgot;
1892 srelgot->size += p->count * sizeof (Elf32_External_Rela);
1899 readonly_dynrelocs (struct elf_link_hash_entry *h)
1901 struct elf_dyn_relocs *p;
1903 for (p = csky_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
1905 asection *s = p->sec->output_section;
1907 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1913 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
1914 read-only sections. */
1917 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
1921 if (h->root.type == bfd_link_hash_indirect)
1924 sec = readonly_dynrelocs (h);
1927 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
1929 info->flags |= DF_TEXTREL;
1930 info->callbacks->minfo
1931 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
1932 sec->owner, h->root.root.string, sec);
1934 /* Not an error, just cut short the traversal. */
1940 /* Set the sizes of the dynamic sections. */
1943 csky_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1944 struct bfd_link_info *info)
1946 struct csky_elf_link_hash_table *htab;
1952 htab = csky_elf_hash_table (info);
1955 dynobj = htab->elf.dynobj;
1959 if (htab->elf.dynamic_sections_created)
1961 /* Set the contents of the .interp section to the interpreter. */
1962 if (!bfd_link_pic (info) && !info->nointerp)
1964 s = bfd_get_section_by_name (dynobj, ".interp");
1965 BFD_ASSERT (s != NULL);
1966 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1967 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1971 /* Set up .got offsets for local syms, and space for local dynamic
1973 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1975 bfd_signed_vma *local_got_refcounts;
1976 bfd_signed_vma *end_local_got;
1977 bfd_size_type locsymcount;
1978 Elf_Internal_Shdr *symtab_hdr;
1979 asection *srelgot, *sgot;
1980 char *local_tls_type;
1982 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1985 sgot = htab->elf.sgot;
1986 srelgot = htab->elf.srelgot;
1988 for (s = ibfd->sections; s != NULL; s = s->next)
1990 struct elf_dyn_relocs *p;
1992 for (p = *((struct elf_dyn_relocs **)
1993 &elf_section_data (s)->local_dynrel);
1997 if (!bfd_is_abs_section (p->sec)
1998 && bfd_is_abs_section (p->sec->output_section))
1999 /* Input section has been discarded, either because
2000 it is a copy of a linkonce section or due to
2001 linker script /DISCARD/, so we'll be discarding
2004 else if (p->count != 0)
2006 srelgot->size += p->count * sizeof (Elf32_External_Rela);
2007 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2008 info->flags |= DF_TEXTREL;
2013 local_got_refcounts = elf_local_got_refcounts (ibfd);
2014 if (!local_got_refcounts)
2017 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2018 locsymcount = symtab_hdr->sh_info;
2019 end_local_got = local_got_refcounts + locsymcount;
2020 local_tls_type = csky_elf_local_got_tls_type (ibfd);
2022 for (; local_got_refcounts < end_local_got;
2023 ++local_got_refcounts, ++local_tls_type)
2025 if (*local_got_refcounts > 0)
2027 /* GOT_TLS_GD and GOT_TLS_IE type for TLS, GOT_NORMAL type
2028 for GOT. If output file is shared library, we should output
2029 GOT_TLS_GD type relocation in .rel.got. */
2030 *local_got_refcounts = sgot->size;
2031 if (*local_tls_type & GOT_TLS_GD)
2032 /* TLS_GD relocs need an 8-byte structure in the GOT. */
2034 if (*local_tls_type & GOT_TLS_IE)
2036 if (*local_tls_type == GOT_NORMAL)
2038 if (bfd_link_pic (info) || *local_tls_type == GOT_TLS_GD)
2039 srelgot->size += sizeof (Elf32_External_Rela);
2042 *local_got_refcounts = (bfd_vma) -1;
2046 if (htab->tls_ldm_got.refcount > 0)
2048 /* Allocate two GOT entries and one dynamic relocation (if necessary)
2049 for R_CSKY_TLS_LDM32 relocations. */
2050 htab->tls_ldm_got.offset = htab->elf.sgot->size;
2051 htab->elf.sgot->size += 8;
2052 if (bfd_link_pic (info))
2053 htab->elf.srelgot->size += sizeof (Elf32_External_Rela);
2056 htab->tls_ldm_got.offset = -1;
2058 /* Allocate global sym .plt and .got entries, and space for global
2059 sym dynamic relocs. */
2060 elf_link_hash_traverse (&htab->elf, csky_allocate_dynrelocs, (PTR) info);
2062 /* Check for GOT overflow. */
2063 if (check_got_overflow == 1
2064 && htab->elf.sgot->size + htab->elf.sgotplt->size > GOT_MAX_SIZE)
2066 _bfd_error_handler (_("GOT table size out of range")); /* */
2070 /* We now have determined the sizes of the various dynamic sections.
2071 Allocate memory for them. */
2073 for (s = dynobj->sections; s != NULL; s = s->next)
2075 bfd_boolean strip_section = TRUE;
2077 if ((s->flags & SEC_LINKER_CREATED) == 0)
2080 if (s == htab->elf.splt
2081 || s == htab->elf.sgot
2082 || s == htab->elf.sgotplt
2083 || s == htab->elf.sdynrelro
2084 || s == htab->elf.sreldynrelro)
2086 /* Strip this section if we don't need it;
2087 see the comment below. */
2088 /* We'd like to strip these sections if they aren't needed, but if
2089 we've exported dynamic symbols from them we must leave them.
2090 It's too late to tell BFD to get rid of the symbols. */
2092 if (htab->elf.hplt != NULL)
2093 strip_section = FALSE;
2095 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel") )
2100 /* We use the reloc_count field as a counter if we need
2101 to copy relocs into the output file. */
2105 /* It's not one of our sections, so don't allocate space. */
2108 /* Strip this section if we don't need it; see the
2112 /* If we don't need this section, strip it from the
2113 output file. This is mostly to handle .rel.bss and
2114 .rel.plt. We must create both sections in
2115 create_dynamic_sections, because they must be created
2116 before the linker maps input sections to output
2117 sections. The linker does that before
2118 adjust_dynamic_symbol is called, and it is that
2119 function which decides whether anything needs to go
2120 into these sections. */
2122 s->flags |= SEC_EXCLUDE;
2126 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2129 /* Allocate memory for the section contents. We use bfd_zalloc
2130 here in case unused entries are not reclaimed before the
2131 section's contents are written out. This should not happen,
2132 but this way if it does, we get a R_CKCORE_NONE reloc instead
2134 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2135 if (s->contents == NULL)
2139 if (htab->elf.dynamic_sections_created)
2141 /* Add some entries to the .dynamic section. We fill in the
2142 values later, in csky_elf_finish_dynamic_sections, but we
2143 must add the entries now so that we get the correct size for
2144 the .dynamic section. The DT_DEBUG entry is filled in by the
2145 dynamic linker and used by the debugger. */
2146 #define add_dynamic_entry(TAG, VAL) \
2147 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2149 if (bfd_link_executable (info) && !add_dynamic_entry (DT_DEBUG, 0))
2152 if (htab->elf.sgot->size != 0 || htab->elf.splt->size)
2154 if (!add_dynamic_entry (DT_PLTGOT, 0)
2155 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2156 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2157 || !add_dynamic_entry (DT_JMPREL, 0))
2163 if (!add_dynamic_entry (DT_RELA, 0)
2164 || !add_dynamic_entry (DT_RELASZ, 0)
2165 || !add_dynamic_entry (DT_RELAENT,
2166 sizeof (Elf32_External_Rela)))
2169 /* If any dynamic relocs apply to a read-only section,
2170 then we need a DT_TEXTREL entry. */
2171 if ((info->flags & DF_TEXTREL) == 0)
2172 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
2174 if ((info->flags & DF_TEXTREL) != 0
2175 && !add_dynamic_entry (DT_TEXTREL, 0))
2179 #undef add_dynamic_entry
2184 /* Finish up dynamic symbol handling. We set the contents of various
2185 dynamic sections here. */
2188 csky_elf_finish_dynamic_symbol (bfd *output_bfd,
2189 struct bfd_link_info *info,
2190 struct elf_link_hash_entry *h,
2191 Elf_Internal_Sym *sym)
2193 struct csky_elf_link_hash_table *htab;
2195 htab = csky_elf_hash_table (info);
2199 /* Sanity check to make sure no unexpected symbol reaches here.
2200 This matches the test in csky_elf_relocate_section handling
2201 of GOT/PLT entries. */
2202 BFD_ASSERT (! (h->dynindx == -1
2204 && h->root.type != bfd_link_hash_undefweak
2205 && bfd_link_pic (info)));
2207 if (h->plt.offset != (bfd_vma) -1)
2211 Elf_Internal_Rela rel;
2213 asection *plt, *relplt, *gotplt;
2215 plt = htab->elf.splt;
2216 relplt = htab->elf.srelplt;
2217 gotplt = htab->elf.sgotplt;
2219 /* This symbol has an entry in the procedure linkage table. Set
2221 BFD_ASSERT (h->dynindx != -1
2222 || ((h->forced_local || bfd_link_executable (info))
2223 && h->def_regular));
2224 BFD_ASSERT (plt != NULL && gotplt != NULL && relplt != NULL);
2225 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
2226 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2228 plt_index = h->plt.offset / PLT_ENTRY_SIZE_P - 1;
2229 got_offset = (plt_index + 3) * 4;
2231 /* Fill in the entry in the procedure linkage table. */
2232 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
2234 csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[0],
2235 plt->contents + h->plt.offset);
2236 csky_put_insn_32 (output_bfd,
2237 (csky_elf_plt_entry_v2[1] | plt_index),
2238 plt->contents + h->plt.offset + 4);
2239 csky_put_insn_32 (output_bfd, csky_elf_plt_entry_v2[2],
2240 plt->contents + h->plt.offset + 8);
2245 for (i = 0; i < 6; i++)
2246 bfd_put_16 (output_bfd, csky_elf_plt_entry_v1[i],
2247 plt->contents + h->plt.offset + i * 2);
2248 bfd_put_32 (output_bfd, plt_index,
2249 plt->contents + h->plt.offset + i * 2);
2252 /* Fill in the entry in the .rel.plt section. */
2253 rel.r_offset = (htab->elf.sgotplt->output_section->vma
2254 + htab->elf.sgotplt->output_offset
2256 rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_JUMP_SLOT);
2257 rel.r_addend = (plt->output_section->vma
2258 + plt->output_offset
2260 loc = (htab->elf.srelplt->contents
2261 + plt_index * sizeof (Elf32_External_Rela));
2264 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2265 if (! h->def_regular)
2267 /* Mark the symbol as undefined, rather than as defined in
2268 the .plt section. Leave the value alone. */
2269 sym->st_shndx = SHN_UNDEF;
2270 /* If the symbol is weak, we do need to clear the value.
2271 Otherwise, the PLT entry would provide a definition for
2272 the symbol even if the symbol wasn't defined anywhere,
2273 and so the symbol would never be NULL. Leave the value if
2274 there were any relocations where pointer equality matters
2275 (this is a clue for the dynamic linker, to make function
2276 pointer comparisons work between an application and shared
2278 if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
2283 /* Fill in the entry in the .got section. */
2284 if (h->got.offset != (bfd_vma) -1
2285 && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_GD) == 0)
2286 && ((csky_elf_hash_entry (h)->tls_type & GOT_TLS_IE) == 0))
2288 Elf_Internal_Rela rel;
2291 /* This symbol has an entry in the global offset table.
2293 BFD_ASSERT (htab->elf.sgot != NULL && htab->elf.srelgot != NULL);
2295 rel.r_offset = (htab->elf.sgot->output_section->vma
2296 + htab->elf.sgot->output_offset
2297 + (h->got.offset & ~(bfd_vma) 1));
2299 /* If this is a static link, or it is a -Bsymbolic link and the
2300 symbol is defined locally or was forced to be local because
2301 of a version file, we just want to emit a RELATIVE reloc.
2302 The entry in the global offset table will already have been
2303 initialized in the relocate_section function. */
2304 if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
2306 BFD_ASSERT ((h->got.offset & 1) != 0);
2307 rel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
2308 rel.r_addend = (h->root.u.def.value
2309 + h->root.u.def.section->output_offset
2310 + h->root.u.def.section->output_section->vma);
2314 BFD_ASSERT ((h->got.offset & 1) == 0);
2315 bfd_put_32 (output_bfd, (bfd_vma) 0,
2316 htab->elf.sgot->contents + h->got.offset);
2317 rel.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_GLOB_DAT);
2321 loc = htab->elf.srelgot->contents;
2322 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
2325 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2331 Elf_Internal_Rela rela;
2334 /* This symbol needs a copy reloc. Set it up. */
2335 BFD_ASSERT (h->dynindx != -1
2336 && (h->root.type == bfd_link_hash_defined
2337 || h->root.type == bfd_link_hash_defweak));
2339 rela.r_offset = (h->root.u.def.value
2340 + h->root.u.def.section->output_section->vma
2341 + h->root.u.def.section->output_offset);
2342 rela.r_info = ELF32_R_INFO (h->dynindx, R_CKCORE_COPY);
2344 if (h->root.u.def.section == htab->elf.sdynrelro)
2345 s = htab->elf.sreldynrelro;
2347 s = htab->elf.srelbss;
2348 BFD_ASSERT (s != NULL);
2349 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2350 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2353 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2354 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2355 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2356 sym->st_shndx = SHN_ABS;
2361 /* Finish up the dynamic sections. */
2364 csky_elf_finish_dynamic_sections (bfd *output_bfd,
2365 struct bfd_link_info *info)
2367 struct csky_elf_link_hash_table *htab;
2372 htab = csky_elf_hash_table (info);
2376 dynobj = htab->elf.dynobj;
2377 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2379 if (htab->elf.dynamic_sections_created)
2381 Elf32_External_Dyn *dyncon, *dynconend;
2383 BFD_ASSERT (sdyn != NULL && htab->elf.sgot != NULL);
2385 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2386 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2387 for (; dyncon < dynconend; dyncon++)
2389 Elf_Internal_Dyn dyn;
2390 bfd_boolean size = FALSE;
2391 const char *name = NULL;
2393 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2411 dyn.d_un.d_ptr = htab->elf.sgot->output_section->vma;
2414 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma
2415 + htab->elf.srelplt->output_offset;
2421 asection *s = bfd_get_section_by_name (output_bfd, name);
2426 dyn.d_un.d_ptr = s->vma;
2428 dyn.d_un.d_val = s->size;
2430 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2434 /* Fill in the first three entries in the global offset table. */
2435 if (htab->elf.sgotplt)
2436 got_sec = htab->elf.sgotplt;
2438 got_sec = htab->elf.sgot;
2439 if (got_sec != NULL)
2441 if (got_sec->size > 0)
2443 bfd_put_32 (output_bfd,
2444 (sdyn == NULL ? (bfd_vma) 0
2445 : sdyn->output_section->vma + sdyn->output_offset),
2447 bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 4);
2448 bfd_put_32 (output_bfd, (bfd_vma) 0, got_sec->contents + 8);
2450 elf_section_data (got_sec->output_section)->this_hdr.sh_entsize = 4;
2455 /* Copy the extra info we tack onto an elf_link_hash_entry. */
2458 csky_elf_copy_indirect_symbol (struct bfd_link_info *info,
2459 struct elf_link_hash_entry *dir,
2460 struct elf_link_hash_entry *ind)
2462 struct csky_elf_link_hash_entry *edir, *eind;
2464 edir = (struct csky_elf_link_hash_entry *) dir;
2465 eind = (struct csky_elf_link_hash_entry *) ind;
2467 if (eind->dyn_relocs != NULL)
2469 if (edir->dyn_relocs != NULL)
2471 struct elf_dyn_relocs **pp;
2472 struct elf_dyn_relocs *p;
2474 /* Add reloc counts against the indirect sym to the direct sym
2475 list. Merge any entries against the same section. */
2476 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
2478 struct elf_dyn_relocs *q;
2480 for (q = edir->dyn_relocs; q != NULL; q = q->next)
2481 if (q->sec == p->sec)
2483 q->pc_count += p->pc_count;
2484 q->count += p->count;
2491 *pp = edir->dyn_relocs;
2493 edir->dyn_relocs = eind->dyn_relocs;
2494 eind->dyn_relocs = NULL;
2496 if (ind->root.type == bfd_link_hash_indirect
2497 && dir->got.refcount <= 0)
2499 edir->tls_type = eind->tls_type;
2500 eind->tls_type = GOT_UNKNOWN;
2502 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2505 /* Used to decide how to sort relocs in an optimal manner for the
2506 dynamic linker, before writing them out. */
2508 static enum elf_reloc_type_class
2509 csky_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2510 const asection *rel_sec ATTRIBUTE_UNUSED,
2511 const Elf_Internal_Rela *rela)
2513 switch ((int) ELF32_R_TYPE (rela->r_info))
2515 case R_CKCORE_RELATIVE:
2516 return reloc_class_relative;
2517 case R_CKCORE_JUMP_SLOT:
2518 return reloc_class_plt;
2520 return reloc_class_copy;
2521 case R_CKCORE_IRELATIVE:
2522 return reloc_class_ifunc;
2524 return reloc_class_normal;
2528 /* Return the section that should be marked against GC for a given
2532 csky_elf_gc_mark_hook (asection *sec,
2533 struct bfd_link_info *info,
2534 Elf_Internal_Rela *rel,
2535 struct elf_link_hash_entry *h,
2536 Elf_Internal_Sym *sym)
2540 switch (ELF32_R_TYPE (rel->r_info))
2542 case R_CKCORE_GNU_VTINHERIT:
2543 case R_CKCORE_GNU_VTENTRY:
2548 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2551 /* Look through the relocs for a section during the first phase.
2552 Since we don't do .gots or .plts, we just need to consider the
2553 virtual table relocs for gc. */
2556 csky_elf_check_relocs (bfd * abfd,
2557 struct bfd_link_info * info,
2559 const Elf_Internal_Rela * relocs)
2561 Elf_Internal_Shdr * symtab_hdr;
2562 struct elf_link_hash_entry ** sym_hashes;
2563 const Elf_Internal_Rela * rel;
2564 const Elf_Internal_Rela * rel_end;
2565 struct csky_elf_link_hash_table *htab;
2568 /* if output type is relocatable, return. */
2569 if (bfd_link_relocatable (info))
2572 htab = csky_elf_hash_table (info);
2576 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
2577 sym_hashes = elf_sym_hashes (abfd);
2579 rel_end = relocs + sec->reloc_count;
2581 for (rel = relocs; rel < rel_end; rel++)
2583 struct elf_link_hash_entry *h;
2584 unsigned long r_symndx;
2585 Elf_Internal_Sym *isym;
2588 r_symndx = ELF32_R_SYM (rel->r_info);
2589 r_type = ELF32_R_TYPE (rel->r_info);
2590 if (r_symndx < symtab_hdr->sh_info)
2592 /* A local symbol. */
2593 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2602 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2603 while (h->root.type == bfd_link_hash_indirect
2604 || h->root.type == bfd_link_hash_warning)
2605 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2610 case R_CKCORE_PCREL_IMM26BY2:
2611 case R_CKCORE_PCREL_IMM11BY2:
2612 case R_CKCORE_PCREL_JSR_IMM11BY2:
2613 case R_CKCORE_PCREL_JSR_IMM26BY2:
2614 /* If the symbol is '*UND*', means this reloc is used for
2615 * callgraph, don't need to leave to shared object. */
2618 /* Else fall through. */
2619 case R_CKCORE_ADDR32:
2620 case R_CKCORE_ADDR_HI16:
2621 case R_CKCORE_ADDR_LO16:
2623 && bfd_link_executable (info)
2624 && r_type == R_CKCORE_ADDR32
2625 && h->type == STT_OBJECT
2626 && (sec->flags & SEC_ALLOC) != 0
2627 && (sec->flags & SEC_READONLY))
2628 /* If this reloc is in a read-only section, we might
2629 need a copy reloc. We can't check reliably at this
2630 stage whether the section is read-only, as input
2631 sections have not yet been mapped to output sections.
2632 Tentatively set the flag for now, and correct in
2633 adjust_dynamic_symbol. */
2636 /* If we are creating a shared library or relocatable executable,
2637 and this is a reloc against a global symbol, then we need to
2638 copy the reloc into the shared library. However, if we are
2639 linking with -Bsymbolic, we do not need to copy a reloc
2640 against a global symbol which is defined in an object we are
2641 including in the link (i.e., DEF_REGULAR is set). At
2642 this point we have not seen all the input files, so it is
2643 possible that DEF_REGULAR is not set now but will be set
2644 later (it is never cleared). We account for that possibility
2645 below by storing information in the relocs_copied field of
2646 the hash table entry. */
2647 if ((bfd_link_pic (info) && (sec->flags & SEC_ALLOC) != 0)
2648 || (!bfd_link_pic (info)
2649 && (sec->flags & SEC_ALLOC) != 0
2651 && (h->root.type == bfd_link_hash_defweak
2652 || !h->def_regular)))
2654 struct elf_dyn_relocs *p;
2655 struct elf_dyn_relocs **head;
2656 /* We must copy these reloc types into the output file.
2657 Create a reloc section in dynobj and make room for
2661 if (htab->elf.dynobj == NULL)
2662 htab->elf.dynobj = abfd;
2664 sreloc = _bfd_elf_make_dynamic_reloc_section
2665 (sec, htab->elf.dynobj, 2, abfd, TRUE);
2671 if (h == NULL && !use_branch_stub
2672 && ((ELF32_R_TYPE (rel->r_info)
2673 == R_CKCORE_PCREL_IMM26BY2)
2674 || (ELF32_R_TYPE (rel->r_info)
2675 == R_CKCORE_PCREL_IMM11BY2)))
2678 /* If this is a global symbol, we count the number of
2679 relocations we need for this symbol. */
2682 struct csky_elf_link_hash_entry *eh;
2683 eh = (struct csky_elf_link_hash_entry *)h;
2684 if ((ELF32_R_TYPE (rel->r_info)
2685 == R_CKCORE_PCREL_JSR_IMM26BY2)
2686 || (ELF32_R_TYPE (rel->r_info)
2687 == R_CKCORE_PCREL_JSR_IMM11BY2))
2688 eh->jsri2bsr_refcount += 1;
2689 head = &eh->dyn_relocs;
2693 /* Track dynamic relocs needed for local syms too.
2694 We really need local syms available to do this
2698 Elf_Internal_Sym *loc_isym;
2700 loc_isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2702 if (loc_isym == NULL)
2704 s = bfd_section_from_elf_index (abfd, loc_isym->st_shndx);
2707 vpp = &elf_section_data (s)->local_dynrel;
2708 head = (struct elf_dyn_relocs **)vpp;
2712 if (p == NULL || p->sec != sec)
2714 bfd_size_type amt = sizeof *p;
2715 p = ((struct elf_dyn_relocs *)
2716 bfd_alloc (htab->elf.dynobj, amt));
2726 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2
2727 || ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM11BY2)
2733 case R_CKCORE_PLT_IMM18BY4:
2734 case R_CKCORE_PLT32:
2735 /* This symbol requires a procedure linkage table entry. We
2736 actually build the entry in adjust_dynamic_symbol,
2737 because this might be a case of linking PIC code which is
2738 never referenced by a dynamic object, in which case we
2739 don't need to generate a procedure linkage table entry
2742 /* If this is a local symbol, we resolve it directly without
2743 creating a procedure linkage table entry. */
2746 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PLT_IMM18BY4)
2747 check_got_overflow = 1;
2750 h->plt.refcount += 1;
2751 h->got.refcount += 1;
2752 ((struct csky_elf_link_hash_entry *)h)->plt_refcount += 1;
2755 case R_CKCORE_GOT12:
2756 case R_CKCORE_PLT12:
2757 case R_CKCORE_GOT32:
2758 case R_CKCORE_GOT_HI16:
2759 case R_CKCORE_GOT_LO16:
2760 case R_CKCORE_PLT_HI16:
2761 case R_CKCORE_PLT_LO16:
2762 case R_CKCORE_GOT_IMM18BY4:
2763 case R_CKCORE_TLS_IE32:
2764 case R_CKCORE_TLS_GD32:
2766 int tls_type, old_tls_type;
2769 && bfd_link_executable (info)
2770 && r_type == R_CKCORE_GOT_IMM18BY4
2771 && (sec->flags & SEC_ALLOC) != 0
2772 && (sec->flags & SEC_READONLY))
2773 /* If this reloc is in a read-only section, we might
2774 need a copy reloc. We can't check reliably at this
2775 stage whether the section is read-only, as input
2776 sections have not yet been mapped to output sections.
2777 Tentatively set the flag for now, and correct in
2778 adjust_dynamic_symbol. */
2781 switch (ELF32_R_TYPE (rel->r_info))
2783 case R_CKCORE_TLS_IE32:
2784 tls_type = GOT_TLS_IE;
2786 case R_CKCORE_TLS_GD32:
2787 tls_type = GOT_TLS_GD;
2790 tls_type = GOT_NORMAL;
2795 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_GOT_IMM18BY4)
2796 check_got_overflow = 1;
2797 h->got.refcount += 1;
2798 old_tls_type = csky_elf_hash_entry (h)->tls_type;
2802 bfd_signed_vma *local_got_refcounts;
2804 /* This is a global offset table entry for a local symbol. */
2805 /* we can write a new function named
2806 elf32_csky_allocate_local_sym_info() to replace
2808 local_got_refcounts = elf_local_got_refcounts (abfd);
2809 if (local_got_refcounts == NULL)
2813 size = symtab_hdr->sh_info;
2814 size *= (sizeof (bfd_signed_vma) + sizeof (char));
2815 local_got_refcounts = ((bfd_signed_vma *)
2816 bfd_zalloc (abfd, size));
2817 if (local_got_refcounts == NULL)
2819 elf_local_got_refcounts (abfd) = local_got_refcounts;
2820 csky_elf_local_got_tls_type (abfd)
2821 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
2823 local_got_refcounts[r_symndx] += 1;
2824 old_tls_type = csky_elf_local_got_tls_type (abfd)[r_symndx];
2827 /* We will already have issued an error message if there is a
2828 TLS / non-TLS mismatch, based on the symbol type. We don't
2829 support any linker relaxations. So just combine any TLS
2831 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
2832 && tls_type != GOT_NORMAL)
2833 tls_type |= old_tls_type;
2835 if (old_tls_type != tls_type)
2838 csky_elf_hash_entry (h)->tls_type = tls_type;
2840 csky_elf_local_got_tls_type (abfd)[r_symndx] = tls_type;
2845 case R_CKCORE_TLS_LDM32:
2846 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_TLS_LDM32)
2847 htab->tls_ldm_got.refcount++;
2850 case R_CKCORE_GOTOFF:
2851 case R_CKCORE_GOTPC:
2852 case R_CKCORE_GOTOFF_HI16:
2853 case R_CKCORE_GOTOFF_LO16:
2854 case R_CKCORE_GOTPC_HI16:
2855 case R_CKCORE_GOTPC_LO16:
2856 case R_CKCORE_GOTOFF_IMM18:
2857 if (htab->elf.sgot == NULL)
2859 if (htab->elf.dynobj == NULL)
2860 htab->elf.dynobj = abfd;
2861 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info))
2866 /* This relocation describes the C++ object vtable hierarchy.
2867 Reconstruct it for later use during GC. */
2868 case R_CKCORE_GNU_VTINHERIT:
2869 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2873 /* This relocation describes which C++ vtable entries are actually
2874 used. Record for later use during GC. */
2875 case R_CKCORE_GNU_VTENTRY:
2876 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2885 static const struct bfd_elf_special_section csky_elf_special_sections[]=
2887 { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2888 { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2889 { NULL, 0, 0, 0, 0 }
2892 /* Function to keep CSKY specific flags in the ELF header. */
2895 csky_elf_set_private_flags (bfd * abfd, flagword flags)
2897 BFD_ASSERT (! elf_flags_init (abfd)
2898 || elf_elfheader (abfd)->e_flags == flags);
2900 elf_elfheader (abfd)->e_flags = flags;
2901 elf_flags_init (abfd) = TRUE;
2905 static csky_arch_for_merge *
2906 csky_find_arch_with_eflag (const unsigned long arch_eflag)
2908 csky_arch_for_merge *csky_arch = NULL;
2910 for (csky_arch = csky_archs; csky_arch->name != NULL; csky_arch++)
2911 if (csky_arch->arch_eflag == arch_eflag)
2913 if (csky_arch == NULL)
2915 _bfd_error_handler (_("warning: unrecognized arch eflag '%#lx'"),
2917 bfd_set_error (bfd_error_wrong_format);
2922 /* Merge backend specific data from an object file to the output
2923 object file when linking. */
2926 csky_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2928 bfd *obfd = info->output_bfd;
2931 csky_arch_for_merge *old_arch = NULL;
2932 csky_arch_for_merge *new_arch = NULL;
2934 /* Check if we have the same endianness. */
2935 if (! _bfd_generic_verify_endian_match (ibfd, info))
2938 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2939 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2942 new_flags = elf_elfheader (ibfd)->e_flags;
2943 old_flags = elf_elfheader (obfd)->e_flags;
2945 if (! elf_flags_init (obfd))
2947 /* First call, no flags set. */
2948 elf_flags_init (obfd) = TRUE;
2949 elf_elfheader (obfd)->e_flags = new_flags;
2951 else if (new_flags == old_flags)
2954 else if (new_flags == 0 || old_flags == 0)
2955 /* When one flag is 0, assign the other one's flag. */
2956 elf_elfheader (obfd)->e_flags = new_flags | old_flags;
2959 flagword newest_flag = 0;
2961 if ((new_flags & CSKY_ARCH_MASK) != 0
2962 && (old_flags & CSKY_ARCH_MASK) != 0)
2964 new_arch = csky_find_arch_with_eflag (new_flags & CSKY_ARCH_MASK);
2965 old_arch = csky_find_arch_with_eflag (old_flags & CSKY_ARCH_MASK);
2966 /* Collect flags like e, f, g. */
2967 newest_flag = (old_flags & (~CSKY_ARCH_MASK))
2968 | (new_flags & (~CSKY_ARCH_MASK));
2969 if (new_arch != NULL && old_arch != NULL)
2971 if (new_arch->class != old_arch->class)
2974 /* xgettext:c-format */
2975 (_("%pB: machine flag conflict with target"), ibfd);
2976 bfd_set_error (bfd_error_wrong_format);
2979 else if (new_arch->class_level != old_arch->class_level)
2981 csky_arch_for_merge *newest_arch
2982 = (new_arch->class_level > old_arch->class_level
2983 ? new_arch : old_arch);
2984 if (new_arch->do_warning || old_arch->do_warning)
2987 /* xgettext:c-format */
2988 (_("warning: file %pB's arch flag ck%s conflicts with "
2989 "target ck%s, using ck%s"),
2990 ibfd, new_arch->name, old_arch->name,
2992 bfd_set_error (bfd_error_wrong_format);
2995 newest_flag |= newest_arch->arch_eflag;
2998 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
3000 & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3003 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
3004 | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3007 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
3008 | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3010 elf_elfheader (obfd)->e_flags = newest_flag;
3015 /* Ignore the discarded relocs in special sections in link time. */
3018 csky_elf_ignore_discarded_relocs (asection *sec)
3020 if (strcmp (sec->name, ".csky_stack_size") == 0)
3025 /* .csky_stack_size are not referenced directly. This pass marks all of
3026 them as required. */
3029 elf32_csky_gc_mark_extra_sections (struct bfd_link_info *info,
3030 elf_gc_mark_hook_fn gc_mark_hook ATTRIBUTE_UNUSED)
3034 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
3036 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3040 for (o = sub->sections; o != NULL; o = o->next)
3041 if (strcmp (o->name, ".csky_stack_size") == 0)
3048 /* The linker repeatedly calls this function for each input section,
3049 in the order that input sections are linked into output sections.
3050 Build lists of input sections to determine groupings between which
3051 we may insert linker stubs. */
3054 elf32_csky_next_input_section (struct bfd_link_info *info,
3057 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3060 if (isec->output_section->index <= htab->top_index)
3062 asection **list = htab->input_list + isec->output_section->index;
3064 if (*list != bfd_abs_section_ptr)
3066 /* Steal the link_sec pointer for our list. */
3067 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3068 /* This happens to make the list in reverse order,
3069 which we reverse later in group_sections. */
3070 PREV_SEC (isec) = *list;
3076 /* See whether we can group stub sections together. Grouping stub
3077 sections may result in fewer stubs. More importantly, we need to
3078 put all .init* and .fini* stubs at the end of the .init or
3079 .fini output sections respectively, because glibc splits the
3080 _init and _fini functions into multiple parts. Putting a stub in
3081 the middle of a function is not a good idea. */
3084 group_sections (struct csky_elf_link_hash_table *htab,
3085 bfd_size_type stub_group_size,
3086 bfd_boolean stubs_always_after_branch)
3088 asection **list = htab->input_list;
3092 asection *tail = *list;
3095 if (tail == bfd_abs_section_ptr)
3098 /* Reverse the list: we must avoid placing stubs at the
3099 beginning of the section because the beginning of the text
3100 section may be required for an interrupt vector in bare metal
3102 #define NEXT_SEC PREV_SEC
3104 while (tail != NULL)
3106 /* Pop from tail. */
3107 asection *item = tail;
3108 tail = PREV_SEC (item);
3111 NEXT_SEC (item) = head;
3115 while (head != NULL)
3119 bfd_vma stub_group_start = head->output_offset;
3120 bfd_vma end_of_next;
3123 while (NEXT_SEC (curr) != NULL)
3125 next = NEXT_SEC (curr);
3126 end_of_next = next->output_offset + next->size;
3127 if (end_of_next - stub_group_start >= stub_group_size)
3128 /* End of NEXT is too far from start, so stop. */
3133 /* OK, the size from the start to the start of CURR is less
3134 * than stub_group_size and thus can be handled by one stub
3135 * section. (Or the head section is itself larger than
3136 * stub_group_size, in which case we may be toast.)
3137 * We should really be keeping track of the total size of
3138 * stubs added here, as stubs contribute to the final output
3142 next = NEXT_SEC (head);
3143 /* Set up this stub group. */
3144 htab->stub_group[head->id].link_sec = curr;
3146 while (head != curr && (head = next) != NULL);
3148 /* But wait, there's more! Input sections up to stub_group_size
3149 * bytes after the stub section can be handled by it too. */
3150 if (!stubs_always_after_branch)
3152 stub_group_start = curr->output_offset + curr->size;
3154 while (next != NULL)
3156 end_of_next = next->output_offset + next->size;
3157 if (end_of_next - stub_group_start >= stub_group_size)
3158 /* End of NEXT is too far from stubs, so stop. */
3160 /* Add NEXT to the stub group. */
3162 next = NEXT_SEC (head);
3163 htab->stub_group[head->id].link_sec = curr;
3169 while (list++ != htab->input_list + htab->top_index);
3171 free (htab->input_list);
3176 /* If the symbol referenced by bsr is defined in shared object file,
3177 or it is a weak symbol and we aim to create shared object file,
3178 we must create a stub for this bsr. */
3181 sym_must_create_stub (struct elf_link_hash_entry *h,
3182 struct bfd_link_info *info)
3185 && ((h->def_dynamic && !h->def_regular)
3186 || (bfd_link_pic (info) && h->root.type == bfd_link_hash_defweak)))
3192 /* Calculate the template, template size and instruction size for a stub.
3193 Return value is the instruction size. */
3196 find_stub_size_and_template (enum elf32_csky_stub_type stub_type,
3197 const insn_sequence **stub_template,
3198 int *stub_template_size)
3200 const insn_sequence *template_sequence = NULL;
3201 int template_size = 0;
3205 template_sequence = stub_definitions[stub_type].template_sequence;
3206 template_size = stub_definitions[stub_type].template_size;
3209 for (i = 0; i < template_size; i++)
3211 switch (template_sequence[i].type)
3229 *stub_template = template_sequence;
3230 if (stub_template_size)
3231 *stub_template_size = template_size;
3236 /* As above, but don't actually build the stub. Just bump offset so
3237 we know stub section sizes. */
3240 csky_size_one_stub (struct bfd_hash_entry *gen_entry,
3241 void * in_arg ATTRIBUTE_UNUSED)
3243 struct elf32_csky_stub_hash_entry *stub_entry;
3244 const insn_sequence *template_sequence = NULL;
3245 int template_size = 0;
3248 /* Massage our args to the form they really have. */
3249 stub_entry = (struct elf32_csky_stub_hash_entry *) gen_entry;
3251 BFD_ASSERT (stub_entry->stub_type > csky_stub_none
3252 && stub_entry->stub_type < ARRAY_SIZE (stub_definitions));
3253 size = find_stub_size_and_template (stub_entry->stub_type,
3254 &template_sequence, &template_size);
3255 stub_entry->stub_size = size;
3256 stub_entry->stub_template = template_sequence;
3257 stub_entry->stub_template_size = template_size;
3259 size = (size + 7) & ~7;
3260 stub_entry->stub_sec->size += size;
3264 /* Add a new stub entry to the stub hash. Not all fields of the new
3265 stub entry are initialised. */
3267 static struct elf32_csky_stub_hash_entry *
3268 elf32_csky_add_stub (const char *stub_name,
3270 struct csky_elf_link_hash_table *htab)
3274 struct elf32_csky_stub_hash_entry *stub_entry;
3276 stub_sec = elf32_csky_create_or_find_stub_sec (&link_sec, section, htab);
3277 if (stub_sec == NULL)
3280 /* Enter this entry into the linker stub hash table. */
3281 stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3283 if (stub_entry == NULL)
3285 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3286 section->owner, stub_name);
3290 stub_entry->stub_sec = stub_sec;
3291 stub_entry->stub_offset = 0;
3292 stub_entry->id_sec = link_sec;
3297 /* Determine and set the size of the stub section for a final link.
3298 The basic idea here is to examine all the relocations looking for
3299 PC-relative calls to a target that is unreachable with a "bsr"
3303 elf32_csky_size_stubs (bfd *output_bfd,
3305 struct bfd_link_info *info,
3306 bfd_signed_vma group_size,
3307 asection *(*add_stub_section) (const char*, asection*),
3308 void (*layout_sections_again) (void))
3310 bfd_size_type stub_group_size;
3311 bfd_boolean stubs_always_after_branch;
3312 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3317 /* Propagate mach to stub bfd, because it may not have been
3318 finalized when we created stub_bfd. */
3319 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3320 bfd_get_mach (output_bfd));
3322 /* Stash our params away. */
3323 htab->stub_bfd = stub_bfd;
3324 htab->add_stub_section = add_stub_section;
3325 htab->layout_sections_again = layout_sections_again;
3326 stubs_always_after_branch = group_size < 0;
3329 stub_group_size = -group_size;
3331 stub_group_size = group_size;
3333 if (stub_group_size == 1)
3334 /* The 'bsr' range in abiv2 is +-64MB has to be used as the
3335 default maximum size.
3336 This value is 128K less than that, which allows for 131072
3337 byte stubs. If we exceed that, then we will fail to link.
3338 The user will have to relink with an explicit group size
3340 stub_group_size = 66977792;
3342 group_sections (htab, stub_group_size, stubs_always_after_branch);
3347 unsigned int bfd_indx;
3349 bfd_boolean stub_changed = FALSE;
3351 for (input_bfd = info->input_bfds, bfd_indx = 0;
3353 input_bfd = input_bfd->link.next, bfd_indx++)
3355 Elf_Internal_Shdr *symtab_hdr;
3357 Elf_Internal_Sym *local_syms = NULL;
3359 /* We'll need the symbol table in a second. */
3360 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3361 if (symtab_hdr->sh_info == 0)
3364 /* Walk over each section attached to the input bfd. */
3365 for (section = input_bfd->sections;
3367 section = section->next)
3369 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3371 /* If there aren't any relocs, then there's nothing more
3373 if ((section->flags & SEC_RELOC) == 0
3374 || section->reloc_count == 0
3375 || (section->flags & SEC_CODE) == 0)
3378 /* If this section is a link-once section that will be
3379 discarded, then don't create any stubs. */
3380 if (section->output_section == NULL
3381 || section->output_section->owner != output_bfd)
3384 /* Get the relocs. */
3385 internal_relocs = _bfd_elf_link_read_relocs (input_bfd,
3390 if (internal_relocs == NULL)
3391 goto error_ret_free_local;
3393 /* Now examine each relocation. */
3394 irela = internal_relocs;
3395 irelaend = irela + section->reloc_count;
3396 for (; irela < irelaend; irela++)
3398 unsigned int r_type, r_indx;
3399 enum elf32_csky_stub_type stub_type;
3400 struct elf32_csky_stub_hash_entry *stub_entry;
3403 bfd_vma destination;
3404 struct csky_elf_link_hash_entry *hash;
3405 const char *sym_name;
3407 const asection *id_sec;
3408 unsigned char st_type;
3410 r_type = ELF32_R_TYPE (irela->r_info);
3411 r_indx = ELF32_R_SYM (irela->r_info);
3412 if (r_type >= (unsigned int) R_CKCORE_MAX)
3414 bfd_set_error (bfd_error_bad_value);
3415 error_ret_free_internal:
3416 if (elf_section_data (section)->relocs == NULL)
3417 free (internal_relocs);
3418 goto error_ret_free_local;
3421 /* Only look for stubs on branch instructions. */
3422 if (r_type != (unsigned int) R_CKCORE_PCREL_IMM26BY2)
3424 /* Now determine the call target, its name, value,
3431 if (r_indx < symtab_hdr->sh_info)
3433 /* It's a local symbol. */
3434 Elf_Internal_Sym *sym;
3435 Elf_Internal_Shdr *hdr;
3436 if (local_syms == NULL)
3438 (Elf_Internal_Sym *) symtab_hdr->contents;
3439 if (local_syms == NULL)
3442 bfd_elf_get_elf_syms (input_bfd,
3444 symtab_hdr->sh_info,
3445 0, NULL, NULL, NULL);
3446 if (local_syms == NULL)
3447 goto error_ret_free_internal;
3449 sym = local_syms + r_indx;
3450 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
3451 sym_sec = hdr->bfd_section;
3453 /* This is an undefined symbol. It can never
3456 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3457 sym_value = sym->st_value;
3458 destination = (sym_value + irela->r_addend
3459 + sym_sec->output_offset
3460 + sym_sec->output_section->vma);
3461 st_type = ELF_ST_TYPE (sym->st_info);
3463 bfd_elf_string_from_elf_section (input_bfd,
3464 symtab_hdr->sh_link,
3469 /* It's an external symbol. */
3471 e_indx = r_indx - symtab_hdr->sh_info;
3472 hash = ((struct csky_elf_link_hash_entry *)
3473 elf_sym_hashes (input_bfd)[e_indx]);
3475 while (hash->elf.root.type == bfd_link_hash_indirect
3476 || hash->elf.root.type == bfd_link_hash_warning)
3477 hash = ((struct csky_elf_link_hash_entry *)
3478 hash->elf.root.u.i.link);
3479 if (hash->elf.root.type == bfd_link_hash_defined
3480 || hash->elf.root.type == bfd_link_hash_defweak)
3482 sym_sec = hash->elf.root.u.def.section;
3483 sym_value = hash->elf.root.u.def.value;
3485 struct csky_elf_link_hash_table *globals =
3486 csky_elf_hash_table (info);
3487 /* FIXME For a destination in a shared library. */
3488 if (globals->elf.splt != NULL && hash != NULL
3489 && hash->elf.plt.offset != (bfd_vma) -1)
3491 else if (sym_sec->output_section != NULL)
3492 destination = (sym_value + irela->r_addend
3493 + sym_sec->output_offset
3494 + sym_sec->output_section->vma);
3496 else if (hash->elf.root.type == bfd_link_hash_undefined
3497 || (hash->elf.root.type
3498 == bfd_link_hash_undefweak))
3499 /* FIXME For a destination in a shared library. */
3503 bfd_set_error (bfd_error_bad_value);
3504 goto error_ret_free_internal;
3506 st_type = ELF_ST_TYPE (hash->elf.type);
3507 sym_name = hash->elf.root.root.string;
3511 /* Determine what (if any) linker stub is needed. */
3512 stub_type = csky_type_of_stub (info, section, irela,
3514 destination, sym_sec,
3515 input_bfd, sym_name);
3516 if (stub_type == csky_stub_none)
3519 /* Support for grouping stub sections. */
3520 id_sec = htab->stub_group[section->id].link_sec;
3522 /* Get the name of this stub. */
3523 stub_name = elf32_csky_stub_name (id_sec, sym_sec, hash,
3526 goto error_ret_free_internal;
3527 /* We've either created a stub for this reloc already,
3528 or we are about to. */
3530 = csky_stub_hash_lookup (&htab->stub_hash_table,
3533 if (stub_entry != NULL)
3535 /* The proper stub has already been created. */
3537 stub_entry->target_value = sym_value;
3540 stub_entry = elf32_csky_add_stub (stub_name, section,
3542 if (stub_entry == NULL)
3545 goto error_ret_free_internal;
3547 stub_entry->target_value = sym_value;
3548 stub_entry->target_section = sym_sec;
3549 stub_entry->stub_type = stub_type;
3550 stub_entry->h = hash;
3551 stub_entry->st_type = st_type;
3553 if (sym_name == NULL)
3554 sym_name = "unnamed";
3555 stub_entry->output_name =
3556 bfd_alloc (htab->stub_bfd,
3557 (sizeof (STUB_ENTRY_NAME)
3558 + strlen (sym_name)));
3559 if (stub_entry->output_name == NULL)
3562 goto error_ret_free_internal;
3564 sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
3566 stub_changed = TRUE;
3570 /* We're done with the internal relocs, free them. */
3571 if (elf_section_data (section)->relocs == NULL)
3572 free (internal_relocs);
3577 /* OK, we've added some stubs. Find out the new size of the
3579 for (stub_sec = htab->stub_bfd->sections;
3581 stub_sec = stub_sec->next)
3583 /* Ignore non-stub sections. */
3584 if (!strstr (stub_sec->name, STUB_SUFFIX))
3588 bfd_hash_traverse (&htab->stub_hash_table, csky_size_one_stub, htab);
3589 /* Ask the linker to do its stuff. */
3590 (*htab->layout_sections_again) ();
3594 error_ret_free_local:
3599 csky_build_one_stub (struct bfd_hash_entry *gen_entry,
3603 struct elf32_csky_stub_hash_entry *stub_entry;
3604 struct bfd_link_info *info;
3611 const insn_sequence *template_sequence;
3613 struct csky_elf_link_hash_table * globals;
3614 int stub_reloc_idx[MAXRELOCS] = {-1, -1};
3615 int stub_reloc_offset[MAXRELOCS] = {0, 0};
3617 struct elf_link_hash_entry *h = NULL;
3619 /* Massage our args to the form they really have. */
3620 stub_entry = (struct elf32_csky_stub_hash_entry *)gen_entry;
3621 info = (struct bfd_link_info *) in_arg;
3623 globals = csky_elf_hash_table (info);
3624 if (globals == NULL)
3626 stub_sec = stub_entry->stub_sec;
3628 /* Make a note of the offset within the stubs for this entry. */
3629 stub_entry->stub_offset = stub_sec->size;
3630 loc = stub_sec->contents + stub_entry->stub_offset;
3632 stub_bfd = stub_sec->owner;
3634 /* This is the address of the stub destination. */
3635 h = &stub_entry->h->elf;
3636 if (sym_must_create_stub (h, info)
3637 && !(bfd_link_pic (info)
3638 && h->root.type == bfd_link_hash_defweak
3640 && !h->def_dynamic))
3643 sym_value = (stub_entry->target_value
3644 + stub_entry->target_section->output_offset
3645 + stub_entry->target_section->output_section->vma);
3647 template_sequence = stub_entry->stub_template;
3648 template_size = stub_entry->stub_template_size;
3651 for (i = 0; i < template_size; i++)
3652 switch (template_sequence[i].type)
3655 bfd_put_16 (stub_bfd, (bfd_vma) template_sequence[i].data,
3660 csky_put_insn_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3665 bfd_put_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3667 stub_reloc_idx[nrelocs] = i;
3668 stub_reloc_offset[nrelocs++] = size;
3675 stub_sec->size += size;
3677 /* Stub size has already been computed in csky_size_one_stub. Check
3679 BFD_ASSERT (size == stub_entry->stub_size);
3681 /* Assume there is at least one and at most MAXRELOCS entries to relocate
3683 BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
3685 for (i = 0; i < nrelocs; i++)
3687 if (sym_must_create_stub (h, info))
3689 Elf_Internal_Rela outrel;
3690 asection * sreloc = globals->elf.srelgot;
3692 outrel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
3694 ELF32_R_INFO (h->dynindx,
3695 template_sequence[stub_reloc_idx[i]].r_type);
3696 outrel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
3698 loc = sreloc->contents;
3699 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3702 bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
3704 _bfd_final_link_relocate (elf32_csky_howto_from_type
3705 (template_sequence[stub_reloc_idx[i]].r_type),
3706 stub_bfd, stub_sec, stub_sec->contents,
3707 stub_entry->stub_offset + stub_reloc_offset[i],
3708 sym_value + stub_entry->target_addend,
3709 template_sequence[stub_reloc_idx[i]].reloc_addend);
3716 /* Build all the stubs associated with the current output file. The
3717 stubs are kept in a hash table attached to the main linker hash
3718 table. We also set up the .plt entries for statically linked PIC
3719 functions here. This function is called via arm_elf_finish in the
3723 elf32_csky_build_stubs (struct bfd_link_info *info)
3726 struct bfd_hash_table *table;
3727 struct csky_elf_link_hash_table *htab;
3729 htab = csky_elf_hash_table (info);
3734 for (stub_sec = htab->stub_bfd->sections;
3736 stub_sec = stub_sec->next)
3740 /* Ignore non-stub sections. */
3741 if (!strstr (stub_sec->name, STUB_SUFFIX))
3744 /* Allocate memory to hold the linker stubs. */
3745 size = stub_sec->size;
3746 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3747 if (stub_sec->contents == NULL && size != 0)
3752 /* Build the stubs as directed by the stub hash table. */
3753 table = &htab->stub_hash_table;
3754 bfd_hash_traverse (table, csky_build_one_stub, info);
3759 /* Set up various things so that we can make a list of input sections
3760 for each output section included in the link. Returns -1 on error,
3761 0 when no stubs will be needed, and 1 on success. */
3764 elf32_csky_setup_section_lists (bfd *output_bfd,
3765 struct bfd_link_info *info)
3768 unsigned int bfd_count;
3769 unsigned int top_id, top_index;
3771 asection **input_list, **list;
3773 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3777 if (! is_elf_hash_table (htab))
3780 /* Count the number of input BFDs and find the top input section id. */
3781 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3783 input_bfd = input_bfd->link.next)
3786 for (section = input_bfd->sections;
3788 section = section->next)
3789 if (top_id < section->id)
3790 top_id = section->id;
3792 htab->bfd_count = bfd_count;
3793 amt = sizeof (struct map_stub) * (top_id + 1);
3794 htab->stub_group = bfd_zmalloc (amt);
3795 if (htab->stub_group == NULL)
3798 /* We can't use output_bfd->section_count here to find the top output
3799 section index as some sections may have been removed, and
3800 _bfd_strip_section_from_output doesn't renumber the indices. */
3801 for (section = output_bfd->sections, top_index = 0;
3803 section = section->next)
3804 if (top_index < section->index)
3805 top_index = section->index;
3806 htab->top_index = top_index;
3807 amt = sizeof (asection *) * (top_index + 1);
3808 input_list = bfd_malloc (amt);
3809 htab->input_list = input_list;
3810 if (input_list == NULL)
3812 /* For sections we aren't interested in, mark their entries with a
3813 value we can check later. */
3814 list = input_list + top_index;
3816 *list = bfd_abs_section_ptr;
3817 while (list-- != input_list);
3818 for (section = output_bfd->sections;
3820 section = section->next)
3821 if ((section->flags & SEC_CODE) != 0)
3822 input_list[section->index] = NULL;
3827 static bfd_reloc_status_type
3828 csky_relocate_contents (reloc_howto_type *howto,
3835 bfd_reloc_status_type flag;
3836 unsigned int rightshift = howto->rightshift;
3837 unsigned int bitpos = howto->bitpos;
3839 /* If the size is negative, negate RELOCATION. This isn't very
3841 if (howto->size < 0)
3842 relocation = -relocation;
3844 /* FIXME: these macros should be defined at file head or head file head. */
3845 #define CSKY_INSN_ADDI_TO_SUBI 0x04000000
3846 #define CSKY_INSN_MOV_RTB 0xc41d4820 // mov32 rx, r29, 0
3847 #define CSKY_INSN_MOV_RDB 0xc41c4820 // mov32 rx, r28, 0
3848 #define CSKY_INSN_GET_ADDI_RZ(x) (((x) & 0x03e00000) >> 21)
3849 #define CSKY_INSN_SET_MOV_RZ(x) ((x) & 0x0000001f)
3850 #define CSKY_INSN_JSRI_TO_LRW 0xea9a0000
3851 #define CSKY_INSN_JSR_R26 0xe8fa0000
3853 /* Get the value we are going to relocate. */
3854 size = bfd_get_reloc_size (howto);
3861 x = bfd_get_8 (input_bfd, location);
3864 x = bfd_get_16 (input_bfd, location);
3867 if (need_reverse_bits)
3869 x = csky_get_insn_32 (input_bfd, location);
3871 if (R_CKCORE_DOFFSET_LO16 == howto->type)
3873 if ((signed) relocation < 0)
3875 x |= CSKY_INSN_ADDI_TO_SUBI;
3876 relocation = -relocation;
3878 else if (0 == relocation)
3879 x = (CSKY_INSN_MOV_RDB |
3880 CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3882 else if (R_CKCORE_TOFFSET_LO16 == howto->type)
3884 if ((signed) relocation < 0)
3886 x |= CSKY_INSN_ADDI_TO_SUBI;
3887 relocation = -relocation;
3889 else if (0 == relocation)
3890 x = (CSKY_INSN_MOV_RTB |
3891 CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3895 x = bfd_get_32 (input_bfd, location);
3898 /* Check for overflow. FIXME: We may drop bits during the addition
3899 which we don't check for. We must either check at every single
3900 operation, which would be tedious, or we must do the computations
3901 in a type larger than bfd_vma, which would be inefficient. */
3902 flag = bfd_reloc_ok;
3903 if (howto->complain_on_overflow != complain_overflow_dont)
3912 /* Get the values to be added together. For signed and unsigned
3913 relocations, we assume that all values should be truncated to
3914 the size of an address. For bitfields, all the bits matter.
3915 See also bfd_check_overflow. */
3916 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
3917 fieldmask = N_ONES (howto->bitsize);
3918 signmask = ~fieldmask;
3919 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3920 a = (relocation & addrmask) >> rightshift;
3921 if (read_content_substitute)
3922 x = read_content_substitute;
3923 b = (x & howto->src_mask & addrmask) >> bitpos;
3925 switch (howto->complain_on_overflow)
3927 case complain_overflow_signed:
3928 /* If any sign bits are set, all sign bits must be set.
3929 That is, A must be a valid negative address after
3931 signmask = ~(fieldmask >> 1);
3934 case complain_overflow_bitfield:
3935 /* Much like the signed check, but for a field one bit
3936 wider. We allow a bitfield to represent numbers in the
3937 range -2**n to 2**n-1, where n is the number of bits in the
3938 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
3939 can't overflow, which is exactly what we want. */
3941 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
3942 flag = bfd_reloc_overflow;
3943 /* We only need this next bit of code if the sign bit of B
3944 is below the sign bit of A. This would only happen if
3945 SRC_MASK had fewer bits than BITSIZE. Note that if
3946 SRC_MASK has more bits than BITSIZE, we can get into
3947 trouble; we would need to verify that B is in range, as
3948 we do for A above. */
3949 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
3952 /* Set all the bits above the sign bit. */
3955 /* Now we can do the addition. */
3958 /* See if the result has the correct sign. Bits above the
3959 sign bit are junk now; ignore them. If the sum is
3960 positive, make sure we did not have all negative inputs;
3961 if the sum is negative, make sure we did not have all
3962 positive inputs. The test below looks only at the sign
3963 bits, and it really just
3964 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3966 We mask with addrmask here to explicitly allow an address
3967 wrap-around. The Linux kernel relies on it, and it is
3968 the only way to write assembler code which can run when
3969 loaded at a location 0x80000000 away from the location at
3970 which it is linked. */
3972 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
3973 flag = bfd_reloc_overflow;
3975 case complain_overflow_unsigned:
3976 /* Checking for an unsigned overflow is relatively easy:
3977 trim the addresses and add, and trim the result as well.
3978 Overflow is normally indicated when the result does not
3979 fit in the field. However, we also need to consider the
3980 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3981 input is 0x80000000, and bfd_vma is only 32 bits; then we
3982 will get sum == 0, but there is an overflow, since the
3983 inputs did not fit in the field. Instead of doing a
3984 separate test, we can check for this by or-ing in the
3985 operands when testing for the sum overflowing its final
3987 sum = (a + b) & addrmask;
3988 if ((a | b | sum) & signmask)
3989 flag = bfd_reloc_overflow;
3996 /* Put RELOCATION in the right bits. */
3997 relocation >>= (bfd_vma) rightshift;
3999 if ((howto->type == R_CKCORE_DOFFSET_LO16
4000 || howto->type == R_CKCORE_TOFFSET_LO16)
4002 /* Do nothing lsli32 rx, rz, 0. */
4006 /* Fir V1, all this relocation must be x -1. */
4007 if (howto->type == R_CKCORE_PCREL_IMM11BY2
4008 || howto->type == R_CKCORE_PCREL_JSR_IMM11BY2
4009 || howto->type == R_CKCORE_DOFFSET_LO16
4010 || howto->type == R_CKCORE_TOFFSET_LO16)
4012 else if (howto->type == R_CKCORE_PCREL_IMM7BY4)
4013 relocation = (relocation & 0x1f) + ((relocation << 3) & 0x300);
4014 else if (howto->type == R_CKCORE_PCREL_FLRW_IMM8BY4)
4016 = ((relocation << 4) & 0xf0) + ((relocation << 17) & 0x1e00000);
4017 else if (howto->type == R_CKCORE_NOJSRI)
4019 x = (x & howto->dst_mask) | CSKY_INSN_JSRI_TO_LRW;
4021 csky_put_insn_32 (input_bfd, CSKY_INSN_JSR_R26, location + 4);
4024 relocation <<= (bfd_vma) bitpos;
4025 /* Add RELOCATION to the right bits of X. */
4026 x = ((x & ~howto->dst_mask)
4027 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4029 /* Put the relocated value back in the object file. */
4035 bfd_put_8 (input_bfd, x, location);
4038 bfd_put_16 (input_bfd, x, location);
4041 if (need_reverse_bits)
4042 csky_put_insn_32 (input_bfd, x, location);
4044 bfd_put_32 (input_bfd, x, location);
4050 /* Look up an entry in the stub hash. Stub entries are cached because
4051 creating the stub name takes a bit of time. */
4053 static struct elf32_csky_stub_hash_entry *
4054 elf32_csky_get_stub_entry (const asection *input_section,
4055 const asection *sym_sec,
4056 struct elf_link_hash_entry *hash,
4057 const Elf_Internal_Rela *rel,
4058 struct csky_elf_link_hash_table *htab)
4060 struct elf32_csky_stub_hash_entry *stub_entry;
4061 struct csky_elf_link_hash_entry *h
4062 = (struct csky_elf_link_hash_entry *) hash;
4063 const asection *id_sec;
4065 if ((input_section->flags & SEC_CODE) == 0)
4068 /* If this input section is part of a group of sections sharing one
4069 stub section, then use the id of the first section in the group.
4070 Stub names need to include a section id, as there may well be
4071 more than one stub used to reach say, printf, and we need to
4072 distinguish between them. */
4073 id_sec = htab->stub_group[input_section->id].link_sec;
4074 if (h != NULL && h->stub_cache != NULL
4075 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
4076 stub_entry = h->stub_cache;
4080 stub_name = elf32_csky_stub_name (id_sec, sym_sec, h, rel);
4081 if (stub_name == NULL)
4083 stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table,
4084 stub_name, FALSE, FALSE);
4086 h->stub_cache = stub_entry;
4093 static bfd_reloc_status_type
4094 csky_final_link_relocate (reloc_howto_type *howto,
4096 asection *input_section,
4104 /* Sanity check the address. */
4105 if (address > bfd_get_section_limit (input_bfd, input_section))
4106 return bfd_reloc_outofrange;
4108 /* This function assumes that we are dealing with a basic relocation
4109 against a symbol. We want to compute the value of the symbol to
4110 relocate to. This is just VALUE, the value of the symbol,
4111 plus ADDEND, any addend associated with the reloc. */
4112 relocation = value + addend;
4114 /* If the relocation is PC relative, we want to set RELOCATION to
4115 the distance between the symbol (currently in RELOCATION) and the
4116 location we are relocating. Some targets (e.g., i386-aout)
4117 arrange for the contents of the section to be the negative of the
4118 offset of the location within the section; for such targets
4119 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
4120 simply leave the contents of the section as zero; for such
4121 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
4122 need to subtract out the offset of the location within the
4123 section (which is just ADDRESS). */
4124 if (howto->pc_relative)
4126 relocation -= (input_section->output_section->vma
4127 + input_section->output_offset);
4128 if (howto->pcrel_offset)
4129 relocation -= address;
4132 return csky_relocate_contents (howto, input_bfd, relocation,
4133 contents + address);
4137 /* Return the base VMA address which should be subtracted from real addresses
4138 when resolving @dtpoff relocation.
4139 This is PT_TLS segment p_vaddr. */
4142 dtpoff_base (struct bfd_link_info *info)
4144 /* If tls_sec is NULL, we should have signalled an error already. */
4145 if (elf_hash_table (info)->tls_sec == NULL)
4147 return elf_hash_table (info)->tls_sec->vma;
4150 /* Return the relocation value for @tpoff relocation
4151 if STT_TLS virtual address is ADDRESS. */
4154 tpoff (struct bfd_link_info *info, bfd_vma address)
4156 struct elf_link_hash_table *htab = elf_hash_table (info);
4159 /* If tls_sec is NULL, we should have signalled an error already. */
4160 if (htab->tls_sec == NULL)
4162 base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
4163 return address - htab->tls_sec->vma + base;
4166 /* Relocate a csky section. */
4169 csky_elf_relocate_section (bfd * output_bfd,
4170 struct bfd_link_info * info,
4172 asection * input_section,
4173 bfd_byte * contents,
4174 Elf_Internal_Rela * relocs,
4175 Elf_Internal_Sym * local_syms,
4176 asection ** local_sections)
4178 Elf_Internal_Shdr *symtab_hdr;
4179 struct elf_link_hash_entry **sym_hashes;
4180 Elf_Internal_Rela *rel;
4181 Elf_Internal_Rela *relend;
4183 bfd_boolean ret = TRUE;
4184 struct csky_elf_link_hash_table * htab;
4185 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
4187 htab = csky_elf_hash_table (info);
4191 symtab_hdr = & elf_symtab_hdr (input_bfd);
4192 sym_hashes = elf_sym_hashes (input_bfd);
4195 relend = relocs + input_section->reloc_count;
4196 for (; rel < relend; rel++)
4198 enum elf_csky_reloc_type r_type
4199 = (enum elf_csky_reloc_type) ELF32_R_TYPE (rel->r_info);
4200 unsigned long r_symndx;
4201 reloc_howto_type * howto;
4202 Elf_Internal_Sym * sym;
4206 struct elf_link_hash_entry * h;
4207 bfd_vma addend = (bfd_vma)rel->r_addend;
4208 bfd_reloc_status_type r = bfd_reloc_ok;
4209 bfd_boolean unresolved_reloc = FALSE;
4210 int do_final_relocate = TRUE;
4211 bfd_boolean relative_reloc = FALSE;
4212 bfd_signed_vma disp;
4214 /* Ignore these relocation types:
4215 R_CKCORE_GNU_VTINHERIT, R_CKCORE_GNU_VTENTRY. */
4216 if (r_type == R_CKCORE_GNU_VTINHERIT || r_type == R_CKCORE_GNU_VTENTRY)
4219 if ((unsigned) r_type >= (unsigned) R_CKCORE_MAX)
4221 /* The r_type is error, not support it. */
4222 /* xgettext:c-format */
4223 _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
4225 bfd_set_error (bfd_error_bad_value);
4230 howto = &csky_elf_howto_table[(int) r_type];
4232 r_symndx = ELF32_R_SYM(rel->r_info);
4236 unresolved_reloc = FALSE;
4238 if (r_symndx < symtab_hdr->sh_info)
4240 /* Get symbol table entry. */
4241 sym = local_syms + r_symndx;
4242 sec = local_sections[r_symndx];
4243 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4244 addend = (bfd_vma)rel->r_addend;
4248 bfd_boolean warned, ignored;
4250 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4251 r_symndx, symtab_hdr, sym_hashes,
4253 unresolved_reloc, warned, ignored);
4256 if (sec != NULL && discarded_section (sec))
4258 /* For relocs against symbols from removed linkonce sections,
4259 or sections discarded by a linker script, we just want the
4260 section contents zeroed. Avoid any special processing.
4261 And if the symbol is referenced in '.csky_stack_size' section,
4262 set the address to SEC_DISCARDED(0xffffffff). */
4264 /* The .csky_stack_size section is just for callgraph. */
4265 if (strcmp (input_section->name, ".csky_stack_size") == 0)
4267 /* FIXME: it should define in head file. */
4268 #define SEC_DISCARDED 0xffffffff
4269 bfd_put_32 (input_bfd, SEC_DISCARDED, contents + rel->r_offset);
4276 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4277 rel, 1, relend, howto, 0,
4281 if (bfd_link_relocatable (info))
4284 read_content_substitute = 0;
4288 + (bfd_signed_vma) addend
4289 - input_section->output_section->vma
4290 - input_section->output_offset
4292 /* It is for ck8xx. */
4293 #define CSKY_INSN_BSR32 0xe0000000
4294 /* It is for ck5xx/ck6xx. */
4295 #define CSKY_INSN_BSR16 0xf800
4296 #define within_range(x, L) (-(1 << (L - 1)) < (x) && (x) < (1 << (L -1)) - 2)
4297 switch (howto->type)
4299 case R_CKCORE_PCREL_IMM18BY2:
4300 /* When h is NULL, means the instruction written as
4302 if the highest bit is set, prevent the high 32bits
4303 turn to 0xffffffff when signed extern in 64bit
4305 if (h == NULL && (addend & 0x80000000))
4306 addend &= 0xffffffff;
4309 case R_CKCORE_PCREL32:
4312 case R_CKCORE_GOT12:
4313 case R_CKCORE_PLT12:
4314 case R_CKCORE_GOT_HI16:
4315 case R_CKCORE_GOT_LO16:
4316 case R_CKCORE_PLT_HI16:
4317 case R_CKCORE_PLT_LO16:
4318 case R_CKCORE_GOT32:
4319 case R_CKCORE_GOT_IMM18BY4:
4320 /* Relocation is to the entry for this symbol in the global
4322 BFD_ASSERT (htab->elf.sgot != NULL);
4325 /* Global symbol is defined by other modules. */
4327 off = h->got.offset;
4328 dyn = htab->elf.dynamic_sections_created;
4329 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4330 bfd_link_pic (info), h)
4331 || (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info,h))
4332 || (ELF_ST_VISIBILITY(h->other)
4333 && h->root.type == bfd_link_hash_undefweak))
4335 /* This is actually a static link, or it is a
4336 -Bsymbolic link and the symbol is defined
4337 locally, or the symbol was forced to be local
4338 because of a version file. We must initialize
4339 this entry in the global offset table. Since the
4340 offset must always be a multiple of 4, we use the
4341 least significant bit to record whether we have
4342 initialized it already.
4343 When doing a dynamic link, we create a .rela.dyn
4344 relocation entry to initialize the value. This
4345 is done in the finish_dynamic_symbol routine. FIXME */
4350 bfd_put_32 (output_bfd, relocation,
4351 htab->elf.sgot->contents + off);
4354 /* TRUE if relative relocation should be generated. GOT reference to
4355 global symbol in PIC will lead to dynamic symbol. It becomes a
4356 problem when "time" or "times" is defined as a variable in an
4357 executable, clashing with functions of the same name in libc. If a
4358 symbol isn't undefined weak symbol, don't make it dynamic in PIC and
4359 generate relative relocation. */
4360 #define GENERATE_RELATIVE_RELOC_P(INFO, H) \
4361 ((H)->dynindx == -1 \
4362 && !(H)->forced_local \
4363 && (H)->root.type != bfd_link_hash_undefweak \
4364 && bfd_link_pic (INFO))
4366 if (GENERATE_RELATIVE_RELOC_P (info, h))
4367 /* If this symbol isn't dynamic
4368 in PIC, generate R_CKCORE_RELATIVE here. */
4369 relative_reloc = TRUE;
4373 unresolved_reloc = FALSE;
4374 } /* End if h != NULL. */
4377 BFD_ASSERT (local_got_offsets != NULL);
4378 off = local_got_offsets[r_symndx];
4380 /* The offset must always be a multiple of 4. We use
4381 the least significant bit to record whether we have
4382 already generated the necessary reloc. */
4387 bfd_put_32 (output_bfd, relocation,
4388 htab->elf.sgot->contents + off);
4389 local_got_offsets[r_symndx] |= 1;
4390 if (bfd_link_pic (info))
4391 relative_reloc = TRUE;
4397 Elf_Internal_Rela outrel;
4400 srelgot = htab->elf.srelgot;
4401 BFD_ASSERT (srelgot != NULL);
4404 = (htab->elf.sgot->output_section->vma
4405 + htab->elf.sgot->output_offset + off);
4406 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4407 outrel.r_addend = relocation;
4408 loc = srelgot->contents;
4409 loc += (srelgot->reloc_count++ * sizeof (Elf32_External_Rela));
4411 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4413 relocation = htab->elf.sgot->output_offset + off;
4416 case R_CKCORE_GOTOFF_IMM18:
4417 case R_CKCORE_GOTOFF:
4418 case R_CKCORE_GOTOFF_HI16:
4419 case R_CKCORE_GOTOFF_LO16:
4420 /* Relocation is relative to the start of the global offset
4422 /* Note that sgot->output_offset is not involved in this
4423 calculation. We always want the start of .got. If we
4424 defined _GLOBAL_OFFSET_TABLE in a different way, as is
4425 permitted by the ABI, we might have to change this
4427 relocation -= htab->elf.sgot->output_section->vma;
4430 case R_CKCORE_GOTPC:
4431 case R_CKCORE_GOTPC_HI16:
4432 case R_CKCORE_GOTPC_LO16:
4433 /* Use global offset table as symbol value. */
4434 relocation = htab->elf.sgot->output_section->vma;
4436 unresolved_reloc = FALSE;
4439 case R_CKCORE_DOFFSET_IMM18:
4440 case R_CKCORE_DOFFSET_IMM18BY2:
4441 case R_CKCORE_DOFFSET_IMM18BY4:
4443 asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4444 relocation -= sdata->output_section->vma;
4448 case R_CKCORE_DOFFSET_LO16:
4450 asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4451 relocation -= sdata->output_section->vma;
4455 case R_CKCORE_TOFFSET_LO16:
4457 asection *stext = bfd_get_section_by_name (output_bfd, ".text");
4459 relocation -= stext->output_section->vma;
4463 case R_CKCORE_PLT_IMM18BY4:
4464 case R_CKCORE_PLT32:
4465 /* Relocation is to the entry for this symbol in the
4466 procedure linkage table. */
4468 /* Resolve a PLT32 reloc against a local symbol directly,
4469 without using the procedure linkage table. */
4473 if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
4475 /* We didn't make a PLT entry for this symbol. This
4476 happens when statically linking PIC code, or when
4477 using -Bsymbolic. */
4478 if (h->got.offset != (bfd_vma) -1)
4482 off = h->got.offset;
4483 dyn = htab->elf.dynamic_sections_created;
4484 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4485 bfd_link_pic (info), h)
4486 || (bfd_link_pic (info)
4487 && SYMBOL_REFERENCES_LOCAL (info, h))
4488 || (ELF_ST_VISIBILITY (h->other)
4489 && h->root.type == bfd_link_hash_undefweak))
4491 /* This is actually a static link, or it is a
4492 -Bsymbolic link and the symbol is defined
4493 locally, or the symbol was forced to be local
4494 because of a version file. We must initialize
4495 this entry in the global offset table. Since the
4496 offset must always be a multiple of 4, we use the
4497 least significant bit to record whether we have
4498 initialized it already.
4500 When doing a dynamic link, we create a .rela.dyn
4501 relocation entry to initialize the value. This
4502 is done in the finish_dynamic_symbol routine.
4509 if (GENERATE_RELATIVE_RELOC_P (info, h))
4510 relative_reloc = TRUE;
4513 bfd_put_32 (output_bfd, relocation,
4514 htab->elf.sgot->contents + off);
4519 Elf_Internal_Rela outrel;
4522 srelgot = htab->elf.srelgot;
4523 BFD_ASSERT (srelgot != NULL);
4526 = (htab->elf.sgot->output_section->vma
4527 + htab->elf.sgot->output_offset + off);
4528 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4529 outrel.r_addend = relocation;
4530 loc = srelgot->contents;
4531 loc += (srelgot->reloc_count++
4532 * sizeof (Elf32_External_Rela));
4534 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4536 relocation = off + htab->elf.sgot->output_offset;
4540 /* The relocation is the got offset. */
4541 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
4542 relocation = (h->plt.offset / PLT_ENTRY_SIZE + 2) * 4;
4544 relocation = (h->plt.offset / PLT_ENTRY_SIZE_P + 2) * 4;
4545 unresolved_reloc = FALSE;
4548 case R_CKCORE_PCREL_IMM26BY2:
4549 case R_CKCORE_PCREL_JSR_IMM26BY2:
4550 case R_CKCORE_PCREL_JSR_IMM11BY2:
4551 case R_CKCORE_PCREL_IMM11BY2:
4552 case R_CKCORE_CALLGRAPH:
4553 /* Emit callgraph information first. */
4554 /* TODO: deal with callgraph. */
4555 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_CALLGRAPH)
4557 /* Some reloc need further handling. */
4558 /* h == NULL means the symbol is a local symbol,
4559 r_symndx == 0 means the symbol is 'ABS' and
4560 the relocation is already handled in assemble,
4561 here just use for callgraph. */
4562 /* TODO: deal with callgraph. */
4563 if (h == NULL && r_symndx == 0)
4565 do_final_relocate = FALSE;
4569 /* Ignore weak references to undefined symbols. */
4570 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
4572 do_final_relocate = FALSE;
4576 /* Using branch stub. */
4577 if (use_branch_stub == TRUE
4578 && ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2)
4580 struct elf32_csky_stub_hash_entry *stub_entry = NULL;
4581 if (sym_must_create_stub (h, info))
4582 stub_entry = elf32_csky_get_stub_entry (input_section,
4585 else if (disp > BSR_MAX_FWD_BRANCH_OFFSET
4586 || disp < BSR_MAX_BWD_BRANCH_OFFSET)
4587 stub_entry = elf32_csky_get_stub_entry (input_section,
4590 if (stub_entry != NULL)
4592 = (stub_entry->stub_offset
4593 + stub_entry->stub_sec->output_offset
4594 + stub_entry->stub_sec->output_section->vma);
4599 || (h->root.type == bfd_link_hash_defined
4600 && h->dynindx == -1)
4601 || ((h->def_regular && !h->def_dynamic)
4602 && (h->root.type != bfd_link_hash_defweak
4603 || ! bfd_link_pic (info))))
4605 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_JSR_IMM26BY2)
4607 if (within_range (disp, 26))
4609 /* In range for BSR32. */
4610 howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM26BY2];
4611 read_content_substitute = CSKY_INSN_BSR32;
4613 else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810)
4614 /* if bsr32 cannot reach, generate
4615 "lrw r25, label; jsr r25" instead of
4617 howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4618 } /* if ELF32_R_TYPE (rel->r_info)... */
4619 else if (ELF32_R_TYPE (rel->r_info)
4620 == R_CKCORE_PCREL_JSR_IMM11BY2)
4622 if (within_range (disp, 11))
4624 /* In range for BSR16. */
4625 howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM11BY2];
4626 read_content_substitute = CSKY_INSN_BSR16;
4630 } /* else if h == NULL... */
4632 else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810
4633 && (ELF32_R_TYPE (rel->r_info)
4634 == R_CKCORE_PCREL_JSR_IMM26BY2))
4636 howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4639 /* Other situation, h->def_dynamic == 1,
4640 undefined_symbol when output file is shared object, etc. */
4641 /* Else fall through. */
4643 case R_CKCORE_ADDR_HI16:
4644 case R_CKCORE_ADDR_LO16:
4645 if (bfd_link_pic (info)
4646 || (!bfd_link_pic (info)
4650 && ((h->def_dynamic && !h->def_regular)
4651 || (htab->elf.dynamic_sections_created
4652 && (h->root.type == bfd_link_hash_undefweak
4653 || h->root.type == bfd_link_hash_undefined
4654 || h->root.type == bfd_link_hash_indirect)))))
4656 Elf_Internal_Rela outrel;
4657 bfd_boolean skip, relocate;
4660 /* When generating a shared object, these relocations
4661 are copied into the output file to be resolved at
4667 _bfd_elf_section_offset (output_bfd, info, input_section,
4669 if (outrel.r_offset == (bfd_vma) -1)
4671 else if (outrel.r_offset == (bfd_vma) -2)
4676 outrel.r_offset += (input_section->output_section->vma
4677 + input_section->output_offset);
4679 memset (&outrel, 0, sizeof (outrel));
4682 && (!bfd_link_pic (info)
4683 || (!SYMBOLIC_BIND (info, h)
4684 && h->root.type == bfd_link_hash_defweak)
4685 || !h->def_regular))
4687 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4688 outrel.r_addend = rel->r_addend;
4692 /* This symbol is local, or marked to become local. */
4694 outrel.r_info = ELF32_R_INFO (0, r_type);
4695 outrel.r_addend = relocation + rel->r_addend;
4697 loc = htab->elf.srelgot->contents;
4698 loc += (htab->elf.srelgot->reloc_count++
4699 * sizeof (Elf32_External_Rela));
4702 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4704 /* If this reloc is against an external symbol, we do not
4705 want to diddle with the addend. Otherwise, we need to
4706 include the symbol value so that it becomes an addend
4707 for the dynamic reloc. */
4710 } /* if bfd_link_pic (info) ... */
4713 case R_CKCORE_ADDR32:
4714 /* r_symndx will be zero only for relocs against symbols
4715 from removed linkonce sections, or sections discarded
4717 This relocation don't nedd to handle, the value will
4718 be set to SEC_DISCARDED(0xffffffff). */
4720 && strcmp (sec->name, ".csky_stack_size") == 0)
4722 do_final_relocate = FALSE;
4725 if (r_symndx >= symtab_hdr->sh_info
4727 && bfd_link_executable (info))
4730 if (r_symndx == 0 || (input_section->flags & SEC_ALLOC) == 0)
4733 if (bfd_link_pic (info)
4736 && ((h->def_dynamic && !h->def_regular)
4737 || (htab->elf.dynamic_sections_created
4738 && (h->root.type == bfd_link_hash_undefweak
4739 || h->root.type == bfd_link_hash_undefined
4740 || h->root.type == bfd_link_hash_indirect)))))
4742 Elf_Internal_Rela outrel;
4743 bfd_boolean skip, relocate;
4746 /* When generating a shared object, these relocations
4747 are copied into the output file to be resolved at
4753 _bfd_elf_section_offset (output_bfd, info, input_section,
4756 if (outrel.r_offset == (bfd_vma) -1)
4758 else if (outrel.r_offset == (bfd_vma) -2)
4764 outrel.r_offset += (input_section->output_section->vma
4765 + input_section->output_offset);
4768 memset (&outrel, 0, sizeof (outrel));
4771 && (!bfd_link_pic (info)
4772 || (!SYMBOLIC_BIND (info, h)
4773 && h->root.type == bfd_link_hash_defweak)
4774 || !h->def_regular))
4776 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4777 outrel.r_addend = rel->r_addend;
4781 /* This symbol is local, or marked to become local. */
4782 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4783 outrel.r_addend = relocation + rel->r_addend;
4786 loc = htab->elf.srelgot->contents;
4787 loc += (htab->elf.srelgot->reloc_count++
4788 * sizeof (Elf32_External_Rela));
4791 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4793 /* If this reloc is against an external symbol, we do
4794 want to diddle with the addend. Otherwise, we need to
4795 include the symbol value so that it becomes an addend
4796 for the dynamic reloc. */
4802 case R_CKCORE_TLS_LDO32:
4803 relocation = relocation - dtpoff_base (info);
4806 case R_CKCORE_TLS_LDM32:
4807 BFD_ASSERT (htab->elf.sgot != NULL);
4808 off = htab->tls_ldm_got.offset;
4813 /* If we don't know the module number,
4814 create a relocation for it. */
4815 if (!bfd_link_executable (info))
4817 Elf_Internal_Rela outrel;
4820 BFD_ASSERT (htab->elf.srelgot != NULL);
4821 outrel.r_addend = 0;
4823 = (htab->elf.sgot->output_section->vma
4824 + htab->elf.sgot->output_offset + off);
4825 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_TLS_DTPMOD32);
4826 bfd_put_32 (output_bfd, outrel.r_addend,
4827 htab->elf.sgot->contents + off);
4829 loc = htab->elf.srelgot->contents;
4830 loc += (htab->elf.srelgot->reloc_count++
4831 * sizeof (Elf32_External_Rela));
4833 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4836 bfd_put_32 (output_bfd, 1,
4837 htab->elf.sgot->contents + off);
4838 htab->tls_ldm_got.offset |= 1;
4841 = (htab->elf.sgot->output_section->vma
4842 + htab->elf.sgot->output_offset + off
4843 - (input_section->output_section->vma
4844 + input_section->output_offset + rel->r_offset));
4846 case R_CKCORE_TLS_LE32:
4847 if (bfd_link_dll (info))
4850 /* xgettext:c-format */
4851 (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
4852 "in shared object"),
4853 input_bfd, input_section, (uint64_t)rel->r_offset,
4858 relocation = tpoff (info, relocation);
4860 case R_CKCORE_TLS_GD32:
4861 case R_CKCORE_TLS_IE32:
4866 BFD_ASSERT (htab->elf.sgot != NULL);
4872 dyn = htab->elf.dynamic_sections_created;
4873 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4874 bfd_link_pic (info), h)
4875 && (!bfd_link_pic (info)
4876 || !SYMBOL_REFERENCES_LOCAL (info, h)))
4878 unresolved_reloc = FALSE;
4881 off = h->got.offset;
4882 tls_type = ((struct csky_elf_link_hash_entry *)h)->tls_type;
4886 BFD_ASSERT (local_got_offsets != NULL);
4887 off = local_got_offsets[r_symndx];
4888 tls_type = csky_elf_local_got_tls_type (input_bfd)[r_symndx];
4891 BFD_ASSERT (tls_type != GOT_UNKNOWN);
4897 bfd_boolean need_relocs = FALSE;
4898 Elf_Internal_Rela outrel;
4899 bfd_byte *loc = NULL;
4901 /* The GOT entries have not been initialized yet. Do it
4902 now, and emit any relocations. If both an IE GOT and a
4903 GD GOT are necessary, we emit the GD first. */
4904 if ((!bfd_link_executable (info) || indx != 0)
4906 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4907 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
4908 || h->root.type != bfd_link_hash_undefined))
4911 BFD_ASSERT (htab->elf.srelgot != NULL);
4913 loc = htab->elf.srelgot->contents;
4914 loc += (htab->elf.srelgot->reloc_count
4915 * sizeof (Elf32_External_Rela));
4917 if (tls_type & GOT_TLS_GD)
4921 outrel.r_addend = 0;
4923 = (htab->elf.sgot->output_section->vma
4924 + htab->elf.sgot->output_offset
4927 = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPMOD32);
4928 bfd_put_32 (output_bfd, outrel.r_addend,
4929 htab->elf.sgot->contents + cur_off);
4931 bfd_elf32_swap_reloca_out (output_bfd,
4933 loc += sizeof (Elf32_External_Rela);
4934 htab->elf.srelgot->reloc_count++;
4936 bfd_put_32 (output_bfd,
4937 relocation - dtpoff_base (info),
4938 (htab->elf.sgot->contents
4942 outrel.r_addend = 0;
4944 = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPOFF32);
4945 outrel.r_offset += 4;
4946 bfd_put_32 (output_bfd, outrel.r_addend,
4947 (htab->elf.sgot->contents
4951 R_CKCORE_TLS_DTPOFF32);
4953 bfd_elf32_swap_reloca_out (output_bfd,
4956 htab->elf.srelgot->reloc_count++;
4957 loc += sizeof (Elf32_External_Rela);
4963 /* If are not emitting relocations for a
4964 general dynamic reference, then we must be in a
4965 static link or an executable link with the
4966 symbol binding locally. Mark it as belonging
4967 to module 1, the executable. */
4968 bfd_put_32 (output_bfd, 1,
4969 htab->elf.sgot->contents + cur_off);
4970 bfd_put_32 (output_bfd,
4971 relocation - dtpoff_base (info),
4972 htab->elf.sgot->contents
4977 if (tls_type & GOT_TLS_IE)
4982 outrel.r_addend = relocation - dtpoff_base (info);
4984 outrel.r_addend = 0;
4986 = (htab->elf.sgot->output_section->vma
4987 + htab->elf.sgot->output_offset + cur_off);
4989 = ELF32_R_INFO (indx, R_CKCORE_TLS_TPOFF32);
4991 bfd_put_32 (output_bfd, outrel.r_addend,
4992 htab->elf.sgot->contents + cur_off);
4994 bfd_elf32_swap_reloca_out (output_bfd,
4996 htab->elf.srelgot->reloc_count++;
4997 loc += sizeof (Elf32_External_Rela);
5000 bfd_put_32 (output_bfd, tpoff (info, relocation),
5001 htab->elf.sgot->contents + cur_off);
5006 local_got_offsets[r_symndx] |= 1;
5008 if ((tls_type & GOT_TLS_GD) && howto->type != R_CKCORE_TLS_GD32)
5011 = (htab->elf.sgot->output_section->vma
5012 + htab->elf.sgot->output_offset + off
5013 - (input_section->output_section->vma
5014 + input_section->output_offset
5019 /* No substitution when final linking. */
5020 read_content_substitute = 0;
5022 } /* End switch (howto->type). */
5024 /* Make sure 32-bit data in the text section will not be affected by
5025 our special endianness.
5026 However, this currently affects noting, since the ADDR32 howto type
5027 does no change with the data read. But we may need this mechanism in
5030 if (howto->size == 2
5031 && (howto->type == R_CKCORE_ADDR32
5032 || howto->type == R_CKCORE_PCREL32
5033 || howto->type == R_CKCORE_GOT32
5034 || howto->type == R_CKCORE_GOTOFF
5035 || howto->type == R_CKCORE_GOTPC
5036 || howto->type == R_CKCORE_PLT32
5037 || howto->type == R_CKCORE_TLS_LE32
5038 || howto->type == R_CKCORE_TLS_IE32
5039 || howto->type == R_CKCORE_TLS_LDM32
5040 || howto->type == R_CKCORE_TLS_GD32
5041 || howto->type == R_CKCORE_TLS_LDO32
5042 || howto->type == R_CKCORE_RELATIVE))
5043 need_reverse_bits = 0;
5045 need_reverse_bits = 1;
5046 /* Do the final link. */
5047 if (howto->type != R_CKCORE_PCREL_JSR_IMM11BY2
5048 && howto->type != R_CKCORE_PCREL_JSR_IMM26BY2
5049 && howto->type != R_CKCORE_CALLGRAPH
5050 && do_final_relocate)
5051 r = csky_final_link_relocate (howto, input_bfd, input_section,
5052 contents, rel->r_offset,
5053 relocation, addend);
5055 if (r != bfd_reloc_ok)
5062 case bfd_reloc_overflow:
5067 name = bfd_elf_string_from_elf_section (input_bfd,
5068 symtab_hdr->sh_link,
5073 name = bfd_section_name (input_bfd, sec);
5075 (*info->callbacks->reloc_overflow)
5077 (h ? &h->root : NULL),
5078 name, howto->name, (bfd_vma) 0,
5079 input_bfd, input_section, rel->r_offset);
5083 } /* End for (;rel < relend; rel++). */
5088 csky_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5093 switch (note->descsz)
5097 /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
5099 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
5100 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5104 /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
5106 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
5107 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5112 /* Make a ".reg/999" section. */
5113 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5114 size, note->descpos + offset);
5118 csky_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5120 switch (note->descsz)
5125 /* Sizeof (struct elf_prpsinfo) on linux csky. */
5127 elf_tdata (abfd)->core->program
5128 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
5129 elf_tdata (abfd)->core->command
5130 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
5133 /* Note that for some reason, a spurious space is tacked
5134 onto the end of the args in some (at least one anyway)
5135 implementations, so strip it off if it exists. */
5137 char *command = elf_tdata (abfd)->core->command;
5138 int n = strlen (command);
5140 if (0 < n && command[n - 1] == ' ')
5141 command[n - 1] = '\0';
5147 /* End of external entry points for sizing and building linker stubs. */
5149 /* CPU-related basic API. */
5150 #define TARGET_BIG_SYM csky_elf32_be_vec
5151 #define TARGET_BIG_NAME "elf32-csky-big"
5152 #define TARGET_LITTLE_SYM csky_elf32_le_vec
5153 #define TARGET_LITTLE_NAME "elf32-csky-little"
5154 #define ELF_ARCH bfd_arch_csky
5155 #define ELF_MACHINE_CODE EM_CSKY
5156 #define ELF_MACHINE_ALT1 EM_CSKY_OLD
5157 #define ELF_MAXPAGESIZE 0x1000
5158 #define elf_info_to_howto csky_elf_info_to_howto
5159 #define elf_info_to_howto_rel NULL
5160 #define elf_backend_special_sections csky_elf_special_sections
5161 #define bfd_elf32_bfd_link_hash_table_create csky_elf_link_hash_table_create
5163 /* Target related API. */
5164 #define bfd_elf32_mkobject csky_elf_mkobject
5165 #define bfd_elf32_bfd_merge_private_bfd_data csky_elf_merge_private_bfd_data
5166 #define bfd_elf32_bfd_set_private_flags csky_elf_set_private_flags
5167 #define elf_backend_copy_indirect_symbol csky_elf_copy_indirect_symbol
5169 /* GC section related API. */
5170 #define elf_backend_can_gc_sections 1
5171 #define elf_backend_gc_mark_hook csky_elf_gc_mark_hook
5172 #define elf_backend_gc_mark_extra_sections elf32_csky_gc_mark_extra_sections
5174 /* Relocation related API. */
5175 #define elf_backend_reloc_type_class csky_elf_reloc_type_class
5176 #define bfd_elf32_bfd_reloc_type_lookup csky_elf_reloc_type_lookup
5177 #define bfd_elf32_bfd_reloc_name_lookup csky_elf_reloc_name_lookup
5178 #define elf_backend_ignore_discarded_relocs csky_elf_ignore_discarded_relocs
5179 #define elf_backend_relocate_section csky_elf_relocate_section
5180 #define elf_backend_check_relocs csky_elf_check_relocs
5182 /* Dynamic relocate related API. */
5183 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
5184 #define elf_backend_adjust_dynamic_symbol csky_elf_adjust_dynamic_symbol
5185 #define elf_backend_size_dynamic_sections csky_elf_size_dynamic_sections
5186 #define elf_backend_finish_dynamic_symbol csky_elf_finish_dynamic_symbol
5187 #define elf_backend_finish_dynamic_sections csky_elf_finish_dynamic_sections
5188 #define elf_backend_rela_normal 1
5189 #define elf_backend_can_refcount 1
5190 #define elf_backend_plt_readonly 1
5191 #define elf_backend_want_got_sym 1
5192 #define elf_backend_want_dynrelro 1
5193 #define elf_backend_got_header_size 12
5194 #define elf_backend_want_got_plt 1
5196 /* C-SKY coredump support. */
5197 #define elf_backend_grok_prstatus csky_elf_grok_prstatus
5198 #define elf_backend_grok_psinfo csky_elf_grok_psinfo
5200 #include "elf32-target.h"