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 BFD_ASSERT (h != NULL);
2878 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2887 static const struct bfd_elf_special_section csky_elf_special_sections[]=
2889 { STRING_COMMA_LEN (".ctors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2890 { STRING_COMMA_LEN (".dtors"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2891 { NULL, 0, 0, 0, 0 }
2894 /* Function to keep CSKY specific flags in the ELF header. */
2897 csky_elf_set_private_flags (bfd * abfd, flagword flags)
2899 BFD_ASSERT (! elf_flags_init (abfd)
2900 || elf_elfheader (abfd)->e_flags == flags);
2902 elf_elfheader (abfd)->e_flags = flags;
2903 elf_flags_init (abfd) = TRUE;
2907 static csky_arch_for_merge *
2908 csky_find_arch_with_eflag (const unsigned long arch_eflag)
2910 csky_arch_for_merge *csky_arch = NULL;
2912 for (csky_arch = csky_archs; csky_arch->name != NULL; csky_arch++)
2913 if (csky_arch->arch_eflag == arch_eflag)
2915 if (csky_arch == NULL)
2917 _bfd_error_handler (_("warning: unrecognized arch eflag '%#lx'"),
2919 bfd_set_error (bfd_error_wrong_format);
2924 /* Merge backend specific data from an object file to the output
2925 object file when linking. */
2928 csky_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2930 bfd *obfd = info->output_bfd;
2933 csky_arch_for_merge *old_arch = NULL;
2934 csky_arch_for_merge *new_arch = NULL;
2936 /* Check if we have the same endianness. */
2937 if (! _bfd_generic_verify_endian_match (ibfd, info))
2940 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2941 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2944 new_flags = elf_elfheader (ibfd)->e_flags;
2945 old_flags = elf_elfheader (obfd)->e_flags;
2947 if (! elf_flags_init (obfd))
2949 /* First call, no flags set. */
2950 elf_flags_init (obfd) = TRUE;
2951 elf_elfheader (obfd)->e_flags = new_flags;
2953 else if (new_flags == old_flags)
2956 else if (new_flags == 0 || old_flags == 0)
2957 /* When one flag is 0, assign the other one's flag. */
2958 elf_elfheader (obfd)->e_flags = new_flags | old_flags;
2961 flagword newest_flag = 0;
2963 if ((new_flags & CSKY_ARCH_MASK) != 0
2964 && (old_flags & CSKY_ARCH_MASK) != 0)
2966 new_arch = csky_find_arch_with_eflag (new_flags & CSKY_ARCH_MASK);
2967 old_arch = csky_find_arch_with_eflag (old_flags & CSKY_ARCH_MASK);
2968 /* Collect flags like e, f, g. */
2969 newest_flag = (old_flags & (~CSKY_ARCH_MASK))
2970 | (new_flags & (~CSKY_ARCH_MASK));
2971 if (new_arch != NULL && old_arch != NULL)
2973 if (new_arch->class != old_arch->class)
2976 /* xgettext:c-format */
2977 (_("%pB: machine flag conflict with target"), ibfd);
2978 bfd_set_error (bfd_error_wrong_format);
2981 else if (new_arch->class_level != old_arch->class_level)
2983 csky_arch_for_merge *newest_arch
2984 = (new_arch->class_level > old_arch->class_level
2985 ? new_arch : old_arch);
2986 if (new_arch->do_warning || old_arch->do_warning)
2989 /* xgettext:c-format */
2990 (_("warning: file %pB's arch flag ck%s conflicts with "
2991 "target ck%s, using ck%s"),
2992 ibfd, new_arch->name, old_arch->name,
2994 bfd_set_error (bfd_error_wrong_format);
2997 newest_flag |= newest_arch->arch_eflag;
3000 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
3002 & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3005 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
3006 | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3009 newest_flag |= ((new_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK))
3010 | (old_flags & (CSKY_ARCH_MASK | CSKY_ABI_MASK)));
3012 elf_elfheader (obfd)->e_flags = newest_flag;
3017 /* Ignore the discarded relocs in special sections in link time. */
3020 csky_elf_ignore_discarded_relocs (asection *sec)
3022 if (strcmp (sec->name, ".csky_stack_size") == 0)
3027 /* .csky_stack_size are not referenced directly. This pass marks all of
3028 them as required. */
3031 elf32_csky_gc_mark_extra_sections (struct bfd_link_info *info,
3032 elf_gc_mark_hook_fn gc_mark_hook ATTRIBUTE_UNUSED)
3036 _bfd_elf_gc_mark_extra_sections (info, gc_mark_hook);
3038 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
3042 for (o = sub->sections; o != NULL; o = o->next)
3043 if (strcmp (o->name, ".csky_stack_size") == 0)
3050 /* The linker repeatedly calls this function for each input section,
3051 in the order that input sections are linked into output sections.
3052 Build lists of input sections to determine groupings between which
3053 we may insert linker stubs. */
3056 elf32_csky_next_input_section (struct bfd_link_info *info,
3059 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3062 if (isec->output_section->index <= htab->top_index)
3064 asection **list = htab->input_list + isec->output_section->index;
3066 if (*list != bfd_abs_section_ptr)
3068 /* Steal the link_sec pointer for our list. */
3069 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3070 /* This happens to make the list in reverse order,
3071 which we reverse later in group_sections. */
3072 PREV_SEC (isec) = *list;
3078 /* See whether we can group stub sections together. Grouping stub
3079 sections may result in fewer stubs. More importantly, we need to
3080 put all .init* and .fini* stubs at the end of the .init or
3081 .fini output sections respectively, because glibc splits the
3082 _init and _fini functions into multiple parts. Putting a stub in
3083 the middle of a function is not a good idea. */
3086 group_sections (struct csky_elf_link_hash_table *htab,
3087 bfd_size_type stub_group_size,
3088 bfd_boolean stubs_always_after_branch)
3090 asection **list = htab->input_list;
3094 asection *tail = *list;
3097 if (tail == bfd_abs_section_ptr)
3100 /* Reverse the list: we must avoid placing stubs at the
3101 beginning of the section because the beginning of the text
3102 section may be required for an interrupt vector in bare metal
3104 #define NEXT_SEC PREV_SEC
3106 while (tail != NULL)
3108 /* Pop from tail. */
3109 asection *item = tail;
3110 tail = PREV_SEC (item);
3113 NEXT_SEC (item) = head;
3117 while (head != NULL)
3121 bfd_vma stub_group_start = head->output_offset;
3122 bfd_vma end_of_next;
3125 while (NEXT_SEC (curr) != NULL)
3127 next = NEXT_SEC (curr);
3128 end_of_next = next->output_offset + next->size;
3129 if (end_of_next - stub_group_start >= stub_group_size)
3130 /* End of NEXT is too far from start, so stop. */
3135 /* OK, the size from the start to the start of CURR is less
3136 * than stub_group_size and thus can be handled by one stub
3137 * section. (Or the head section is itself larger than
3138 * stub_group_size, in which case we may be toast.)
3139 * We should really be keeping track of the total size of
3140 * stubs added here, as stubs contribute to the final output
3144 next = NEXT_SEC (head);
3145 /* Set up this stub group. */
3146 htab->stub_group[head->id].link_sec = curr;
3148 while (head != curr && (head = next) != NULL);
3150 /* But wait, there's more! Input sections up to stub_group_size
3151 * bytes after the stub section can be handled by it too. */
3152 if (!stubs_always_after_branch)
3154 stub_group_start = curr->output_offset + curr->size;
3156 while (next != NULL)
3158 end_of_next = next->output_offset + next->size;
3159 if (end_of_next - stub_group_start >= stub_group_size)
3160 /* End of NEXT is too far from stubs, so stop. */
3162 /* Add NEXT to the stub group. */
3164 next = NEXT_SEC (head);
3165 htab->stub_group[head->id].link_sec = curr;
3171 while (list++ != htab->input_list + htab->top_index);
3173 free (htab->input_list);
3178 /* If the symbol referenced by bsr is defined in shared object file,
3179 or it is a weak symbol and we aim to create shared object file,
3180 we must create a stub for this bsr. */
3183 sym_must_create_stub (struct elf_link_hash_entry *h,
3184 struct bfd_link_info *info)
3187 && ((h->def_dynamic && !h->def_regular)
3188 || (bfd_link_pic (info) && h->root.type == bfd_link_hash_defweak)))
3194 /* Calculate the template, template size and instruction size for a stub.
3195 Return value is the instruction size. */
3198 find_stub_size_and_template (enum elf32_csky_stub_type stub_type,
3199 const insn_sequence **stub_template,
3200 int *stub_template_size)
3202 const insn_sequence *template_sequence = NULL;
3203 int template_size = 0;
3207 template_sequence = stub_definitions[stub_type].template_sequence;
3208 template_size = stub_definitions[stub_type].template_size;
3211 for (i = 0; i < template_size; i++)
3213 switch (template_sequence[i].type)
3231 *stub_template = template_sequence;
3232 if (stub_template_size)
3233 *stub_template_size = template_size;
3238 /* As above, but don't actually build the stub. Just bump offset so
3239 we know stub section sizes. */
3242 csky_size_one_stub (struct bfd_hash_entry *gen_entry,
3243 void * in_arg ATTRIBUTE_UNUSED)
3245 struct elf32_csky_stub_hash_entry *stub_entry;
3246 const insn_sequence *template_sequence = NULL;
3247 int template_size = 0;
3250 /* Massage our args to the form they really have. */
3251 stub_entry = (struct elf32_csky_stub_hash_entry *) gen_entry;
3253 BFD_ASSERT (stub_entry->stub_type > csky_stub_none
3254 && stub_entry->stub_type < ARRAY_SIZE (stub_definitions));
3255 size = find_stub_size_and_template (stub_entry->stub_type,
3256 &template_sequence, &template_size);
3257 stub_entry->stub_size = size;
3258 stub_entry->stub_template = template_sequence;
3259 stub_entry->stub_template_size = template_size;
3261 size = (size + 7) & ~7;
3262 stub_entry->stub_sec->size += size;
3266 /* Add a new stub entry to the stub hash. Not all fields of the new
3267 stub entry are initialised. */
3269 static struct elf32_csky_stub_hash_entry *
3270 elf32_csky_add_stub (const char *stub_name,
3272 struct csky_elf_link_hash_table *htab)
3276 struct elf32_csky_stub_hash_entry *stub_entry;
3278 stub_sec = elf32_csky_create_or_find_stub_sec (&link_sec, section, htab);
3279 if (stub_sec == NULL)
3282 /* Enter this entry into the linker stub hash table. */
3283 stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3285 if (stub_entry == NULL)
3287 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3288 section->owner, stub_name);
3292 stub_entry->stub_sec = stub_sec;
3293 stub_entry->stub_offset = 0;
3294 stub_entry->id_sec = link_sec;
3299 /* Determine and set the size of the stub section for a final link.
3300 The basic idea here is to examine all the relocations looking for
3301 PC-relative calls to a target that is unreachable with a "bsr"
3305 elf32_csky_size_stubs (bfd *output_bfd,
3307 struct bfd_link_info *info,
3308 bfd_signed_vma group_size,
3309 asection *(*add_stub_section) (const char*, asection*),
3310 void (*layout_sections_again) (void))
3312 bfd_size_type stub_group_size;
3313 bfd_boolean stubs_always_after_branch;
3314 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3319 /* Propagate mach to stub bfd, because it may not have been
3320 finalized when we created stub_bfd. */
3321 bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
3322 bfd_get_mach (output_bfd));
3324 /* Stash our params away. */
3325 htab->stub_bfd = stub_bfd;
3326 htab->add_stub_section = add_stub_section;
3327 htab->layout_sections_again = layout_sections_again;
3328 stubs_always_after_branch = group_size < 0;
3331 stub_group_size = -group_size;
3333 stub_group_size = group_size;
3335 if (stub_group_size == 1)
3336 /* The 'bsr' range in abiv2 is +-64MB has to be used as the
3337 default maximum size.
3338 This value is 128K less than that, which allows for 131072
3339 byte stubs. If we exceed that, then we will fail to link.
3340 The user will have to relink with an explicit group size
3342 stub_group_size = 66977792;
3344 group_sections (htab, stub_group_size, stubs_always_after_branch);
3349 unsigned int bfd_indx;
3351 bfd_boolean stub_changed = FALSE;
3353 for (input_bfd = info->input_bfds, bfd_indx = 0;
3355 input_bfd = input_bfd->link.next, bfd_indx++)
3357 Elf_Internal_Shdr *symtab_hdr;
3359 Elf_Internal_Sym *local_syms = NULL;
3361 /* We'll need the symbol table in a second. */
3362 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3363 if (symtab_hdr->sh_info == 0)
3366 /* Walk over each section attached to the input bfd. */
3367 for (section = input_bfd->sections;
3369 section = section->next)
3371 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3373 /* If there aren't any relocs, then there's nothing more
3375 if ((section->flags & SEC_RELOC) == 0
3376 || section->reloc_count == 0
3377 || (section->flags & SEC_CODE) == 0)
3380 /* If this section is a link-once section that will be
3381 discarded, then don't create any stubs. */
3382 if (section->output_section == NULL
3383 || section->output_section->owner != output_bfd)
3386 /* Get the relocs. */
3387 internal_relocs = _bfd_elf_link_read_relocs (input_bfd,
3392 if (internal_relocs == NULL)
3393 goto error_ret_free_local;
3395 /* Now examine each relocation. */
3396 irela = internal_relocs;
3397 irelaend = irela + section->reloc_count;
3398 for (; irela < irelaend; irela++)
3400 unsigned int r_type, r_indx;
3401 enum elf32_csky_stub_type stub_type;
3402 struct elf32_csky_stub_hash_entry *stub_entry;
3405 bfd_vma destination;
3406 struct csky_elf_link_hash_entry *hash;
3407 const char *sym_name;
3409 const asection *id_sec;
3410 unsigned char st_type;
3412 r_type = ELF32_R_TYPE (irela->r_info);
3413 r_indx = ELF32_R_SYM (irela->r_info);
3414 if (r_type >= (unsigned int) R_CKCORE_MAX)
3416 bfd_set_error (bfd_error_bad_value);
3417 error_ret_free_internal:
3418 if (elf_section_data (section)->relocs == NULL)
3419 free (internal_relocs);
3420 goto error_ret_free_local;
3423 /* Only look for stubs on branch instructions. */
3424 if (r_type != (unsigned int) R_CKCORE_PCREL_IMM26BY2)
3426 /* Now determine the call target, its name, value,
3433 if (r_indx < symtab_hdr->sh_info)
3435 /* It's a local symbol. */
3436 Elf_Internal_Sym *sym;
3437 Elf_Internal_Shdr *hdr;
3438 if (local_syms == NULL)
3440 (Elf_Internal_Sym *) symtab_hdr->contents;
3441 if (local_syms == NULL)
3444 bfd_elf_get_elf_syms (input_bfd,
3446 symtab_hdr->sh_info,
3447 0, NULL, NULL, NULL);
3448 if (local_syms == NULL)
3449 goto error_ret_free_internal;
3451 sym = local_syms + r_indx;
3452 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
3453 sym_sec = hdr->bfd_section;
3455 /* This is an undefined symbol. It can never
3458 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3459 sym_value = sym->st_value;
3460 destination = (sym_value + irela->r_addend
3461 + sym_sec->output_offset
3462 + sym_sec->output_section->vma);
3463 st_type = ELF_ST_TYPE (sym->st_info);
3465 bfd_elf_string_from_elf_section (input_bfd,
3466 symtab_hdr->sh_link,
3471 /* It's an external symbol. */
3473 e_indx = r_indx - symtab_hdr->sh_info;
3474 hash = ((struct csky_elf_link_hash_entry *)
3475 elf_sym_hashes (input_bfd)[e_indx]);
3477 while (hash->elf.root.type == bfd_link_hash_indirect
3478 || hash->elf.root.type == bfd_link_hash_warning)
3479 hash = ((struct csky_elf_link_hash_entry *)
3480 hash->elf.root.u.i.link);
3481 if (hash->elf.root.type == bfd_link_hash_defined
3482 || hash->elf.root.type == bfd_link_hash_defweak)
3484 sym_sec = hash->elf.root.u.def.section;
3485 sym_value = hash->elf.root.u.def.value;
3487 struct csky_elf_link_hash_table *globals =
3488 csky_elf_hash_table (info);
3489 /* FIXME For a destination in a shared library. */
3490 if (globals->elf.splt != NULL && hash != NULL
3491 && hash->elf.plt.offset != (bfd_vma) -1)
3493 else if (sym_sec->output_section != NULL)
3494 destination = (sym_value + irela->r_addend
3495 + sym_sec->output_offset
3496 + sym_sec->output_section->vma);
3498 else if (hash->elf.root.type == bfd_link_hash_undefined
3499 || (hash->elf.root.type
3500 == bfd_link_hash_undefweak))
3501 /* FIXME For a destination in a shared library. */
3505 bfd_set_error (bfd_error_bad_value);
3506 goto error_ret_free_internal;
3508 st_type = ELF_ST_TYPE (hash->elf.type);
3509 sym_name = hash->elf.root.root.string;
3513 /* Determine what (if any) linker stub is needed. */
3514 stub_type = csky_type_of_stub (info, section, irela,
3516 destination, sym_sec,
3517 input_bfd, sym_name);
3518 if (stub_type == csky_stub_none)
3521 /* Support for grouping stub sections. */
3522 id_sec = htab->stub_group[section->id].link_sec;
3524 /* Get the name of this stub. */
3525 stub_name = elf32_csky_stub_name (id_sec, sym_sec, hash,
3528 goto error_ret_free_internal;
3529 /* We've either created a stub for this reloc already,
3530 or we are about to. */
3532 = csky_stub_hash_lookup (&htab->stub_hash_table,
3535 if (stub_entry != NULL)
3537 /* The proper stub has already been created. */
3539 stub_entry->target_value = sym_value;
3542 stub_entry = elf32_csky_add_stub (stub_name, section,
3544 if (stub_entry == NULL)
3547 goto error_ret_free_internal;
3549 stub_entry->target_value = sym_value;
3550 stub_entry->target_section = sym_sec;
3551 stub_entry->stub_type = stub_type;
3552 stub_entry->h = hash;
3553 stub_entry->st_type = st_type;
3555 if (sym_name == NULL)
3556 sym_name = "unnamed";
3557 stub_entry->output_name =
3558 bfd_alloc (htab->stub_bfd,
3559 (sizeof (STUB_ENTRY_NAME)
3560 + strlen (sym_name)));
3561 if (stub_entry->output_name == NULL)
3564 goto error_ret_free_internal;
3566 sprintf (stub_entry->output_name, STUB_ENTRY_NAME,
3568 stub_changed = TRUE;
3572 /* We're done with the internal relocs, free them. */
3573 if (elf_section_data (section)->relocs == NULL)
3574 free (internal_relocs);
3579 /* OK, we've added some stubs. Find out the new size of the
3581 for (stub_sec = htab->stub_bfd->sections;
3583 stub_sec = stub_sec->next)
3585 /* Ignore non-stub sections. */
3586 if (!strstr (stub_sec->name, STUB_SUFFIX))
3590 bfd_hash_traverse (&htab->stub_hash_table, csky_size_one_stub, htab);
3591 /* Ask the linker to do its stuff. */
3592 (*htab->layout_sections_again) ();
3596 error_ret_free_local:
3601 csky_build_one_stub (struct bfd_hash_entry *gen_entry,
3605 struct elf32_csky_stub_hash_entry *stub_entry;
3606 struct bfd_link_info *info;
3613 const insn_sequence *template_sequence;
3615 struct csky_elf_link_hash_table * globals;
3616 int stub_reloc_idx[MAXRELOCS] = {-1, -1};
3617 int stub_reloc_offset[MAXRELOCS] = {0, 0};
3619 struct elf_link_hash_entry *h = NULL;
3621 /* Massage our args to the form they really have. */
3622 stub_entry = (struct elf32_csky_stub_hash_entry *)gen_entry;
3623 info = (struct bfd_link_info *) in_arg;
3625 globals = csky_elf_hash_table (info);
3626 if (globals == NULL)
3628 stub_sec = stub_entry->stub_sec;
3630 /* Make a note of the offset within the stubs for this entry. */
3631 stub_entry->stub_offset = stub_sec->size;
3632 loc = stub_sec->contents + stub_entry->stub_offset;
3634 stub_bfd = stub_sec->owner;
3636 /* This is the address of the stub destination. */
3637 h = &stub_entry->h->elf;
3638 if (sym_must_create_stub (h, info)
3639 && !(bfd_link_pic (info)
3640 && h->root.type == bfd_link_hash_defweak
3642 && !h->def_dynamic))
3645 sym_value = (stub_entry->target_value
3646 + stub_entry->target_section->output_offset
3647 + stub_entry->target_section->output_section->vma);
3649 template_sequence = stub_entry->stub_template;
3650 template_size = stub_entry->stub_template_size;
3653 for (i = 0; i < template_size; i++)
3654 switch (template_sequence[i].type)
3657 bfd_put_16 (stub_bfd, (bfd_vma) template_sequence[i].data,
3662 csky_put_insn_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3667 bfd_put_32 (stub_bfd, (bfd_vma) template_sequence[i].data,
3669 stub_reloc_idx[nrelocs] = i;
3670 stub_reloc_offset[nrelocs++] = size;
3677 stub_sec->size += size;
3679 /* Stub size has already been computed in csky_size_one_stub. Check
3681 BFD_ASSERT (size == stub_entry->stub_size);
3683 /* Assume there is at least one and at most MAXRELOCS entries to relocate
3685 BFD_ASSERT (nrelocs != 0 && nrelocs <= MAXRELOCS);
3687 for (i = 0; i < nrelocs; i++)
3689 if (sym_must_create_stub (h, info))
3691 Elf_Internal_Rela outrel;
3692 asection * sreloc = globals->elf.srelgot;
3694 outrel.r_offset = stub_entry->stub_offset + stub_reloc_offset[i];
3696 ELF32_R_INFO (h->dynindx,
3697 template_sequence[stub_reloc_idx[i]].r_type);
3698 outrel.r_addend = template_sequence[stub_reloc_idx[i]].reloc_addend;
3700 loc = sreloc->contents;
3701 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3704 bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
3706 _bfd_final_link_relocate (elf32_csky_howto_from_type
3707 (template_sequence[stub_reloc_idx[i]].r_type),
3708 stub_bfd, stub_sec, stub_sec->contents,
3709 stub_entry->stub_offset + stub_reloc_offset[i],
3710 sym_value + stub_entry->target_addend,
3711 template_sequence[stub_reloc_idx[i]].reloc_addend);
3718 /* Build all the stubs associated with the current output file. The
3719 stubs are kept in a hash table attached to the main linker hash
3720 table. We also set up the .plt entries for statically linked PIC
3721 functions here. This function is called via arm_elf_finish in the
3725 elf32_csky_build_stubs (struct bfd_link_info *info)
3728 struct bfd_hash_table *table;
3729 struct csky_elf_link_hash_table *htab;
3731 htab = csky_elf_hash_table (info);
3736 for (stub_sec = htab->stub_bfd->sections;
3738 stub_sec = stub_sec->next)
3742 /* Ignore non-stub sections. */
3743 if (!strstr (stub_sec->name, STUB_SUFFIX))
3746 /* Allocate memory to hold the linker stubs. */
3747 size = stub_sec->size;
3748 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3749 if (stub_sec->contents == NULL && size != 0)
3754 /* Build the stubs as directed by the stub hash table. */
3755 table = &htab->stub_hash_table;
3756 bfd_hash_traverse (table, csky_build_one_stub, info);
3761 /* Set up various things so that we can make a list of input sections
3762 for each output section included in the link. Returns -1 on error,
3763 0 when no stubs will be needed, and 1 on success. */
3766 elf32_csky_setup_section_lists (bfd *output_bfd,
3767 struct bfd_link_info *info)
3770 unsigned int bfd_count;
3771 unsigned int top_id, top_index;
3773 asection **input_list, **list;
3775 struct csky_elf_link_hash_table *htab = csky_elf_hash_table (info);
3779 if (! is_elf_hash_table (htab))
3782 /* Count the number of input BFDs and find the top input section id. */
3783 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3785 input_bfd = input_bfd->link.next)
3788 for (section = input_bfd->sections;
3790 section = section->next)
3791 if (top_id < section->id)
3792 top_id = section->id;
3794 htab->bfd_count = bfd_count;
3795 amt = sizeof (struct map_stub) * (top_id + 1);
3796 htab->stub_group = bfd_zmalloc (amt);
3797 if (htab->stub_group == NULL)
3800 /* We can't use output_bfd->section_count here to find the top output
3801 section index as some sections may have been removed, and
3802 _bfd_strip_section_from_output doesn't renumber the indices. */
3803 for (section = output_bfd->sections, top_index = 0;
3805 section = section->next)
3806 if (top_index < section->index)
3807 top_index = section->index;
3808 htab->top_index = top_index;
3809 amt = sizeof (asection *) * (top_index + 1);
3810 input_list = bfd_malloc (amt);
3811 htab->input_list = input_list;
3812 if (input_list == NULL)
3814 /* For sections we aren't interested in, mark their entries with a
3815 value we can check later. */
3816 list = input_list + top_index;
3818 *list = bfd_abs_section_ptr;
3819 while (list-- != input_list);
3820 for (section = output_bfd->sections;
3822 section = section->next)
3823 if ((section->flags & SEC_CODE) != 0)
3824 input_list[section->index] = NULL;
3829 static bfd_reloc_status_type
3830 csky_relocate_contents (reloc_howto_type *howto,
3837 bfd_reloc_status_type flag;
3838 unsigned int rightshift = howto->rightshift;
3839 unsigned int bitpos = howto->bitpos;
3841 /* If the size is negative, negate RELOCATION. This isn't very
3843 if (howto->size < 0)
3844 relocation = -relocation;
3846 /* FIXME: these macros should be defined at file head or head file head. */
3847 #define CSKY_INSN_ADDI_TO_SUBI 0x04000000
3848 #define CSKY_INSN_MOV_RTB 0xc41d4820 // mov32 rx, r29, 0
3849 #define CSKY_INSN_MOV_RDB 0xc41c4820 // mov32 rx, r28, 0
3850 #define CSKY_INSN_GET_ADDI_RZ(x) (((x) & 0x03e00000) >> 21)
3851 #define CSKY_INSN_SET_MOV_RZ(x) ((x) & 0x0000001f)
3852 #define CSKY_INSN_JSRI_TO_LRW 0xea9a0000
3853 #define CSKY_INSN_JSR_R26 0xe8fa0000
3855 /* Get the value we are going to relocate. */
3856 size = bfd_get_reloc_size (howto);
3863 x = bfd_get_8 (input_bfd, location);
3866 x = bfd_get_16 (input_bfd, location);
3869 if (need_reverse_bits)
3871 x = csky_get_insn_32 (input_bfd, location);
3873 if (R_CKCORE_DOFFSET_LO16 == howto->type)
3875 if ((signed) relocation < 0)
3877 x |= CSKY_INSN_ADDI_TO_SUBI;
3878 relocation = -relocation;
3880 else if (0 == relocation)
3881 x = (CSKY_INSN_MOV_RDB |
3882 CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3884 else if (R_CKCORE_TOFFSET_LO16 == howto->type)
3886 if ((signed) relocation < 0)
3888 x |= CSKY_INSN_ADDI_TO_SUBI;
3889 relocation = -relocation;
3891 else if (0 == relocation)
3892 x = (CSKY_INSN_MOV_RTB |
3893 CSKY_INSN_SET_MOV_RZ (CSKY_INSN_GET_ADDI_RZ (x)));
3897 x = bfd_get_32 (input_bfd, location);
3900 /* Check for overflow. FIXME: We may drop bits during the addition
3901 which we don't check for. We must either check at every single
3902 operation, which would be tedious, or we must do the computations
3903 in a type larger than bfd_vma, which would be inefficient. */
3904 flag = bfd_reloc_ok;
3905 if (howto->complain_on_overflow != complain_overflow_dont)
3914 /* Get the values to be added together. For signed and unsigned
3915 relocations, we assume that all values should be truncated to
3916 the size of an address. For bitfields, all the bits matter.
3917 See also bfd_check_overflow. */
3918 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
3919 fieldmask = N_ONES (howto->bitsize);
3920 signmask = ~fieldmask;
3921 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
3922 a = (relocation & addrmask) >> rightshift;
3923 if (read_content_substitute)
3924 x = read_content_substitute;
3925 b = (x & howto->src_mask & addrmask) >> bitpos;
3927 switch (howto->complain_on_overflow)
3929 case complain_overflow_signed:
3930 /* If any sign bits are set, all sign bits must be set.
3931 That is, A must be a valid negative address after
3933 signmask = ~(fieldmask >> 1);
3936 case complain_overflow_bitfield:
3937 /* Much like the signed check, but for a field one bit
3938 wider. We allow a bitfield to represent numbers in the
3939 range -2**n to 2**n-1, where n is the number of bits in the
3940 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
3941 can't overflow, which is exactly what we want. */
3943 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
3944 flag = bfd_reloc_overflow;
3945 /* We only need this next bit of code if the sign bit of B
3946 is below the sign bit of A. This would only happen if
3947 SRC_MASK had fewer bits than BITSIZE. Note that if
3948 SRC_MASK has more bits than BITSIZE, we can get into
3949 trouble; we would need to verify that B is in range, as
3950 we do for A above. */
3951 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
3954 /* Set all the bits above the sign bit. */
3957 /* Now we can do the addition. */
3960 /* See if the result has the correct sign. Bits above the
3961 sign bit are junk now; ignore them. If the sum is
3962 positive, make sure we did not have all negative inputs;
3963 if the sum is negative, make sure we did not have all
3964 positive inputs. The test below looks only at the sign
3965 bits, and it really just
3966 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
3968 We mask with addrmask here to explicitly allow an address
3969 wrap-around. The Linux kernel relies on it, and it is
3970 the only way to write assembler code which can run when
3971 loaded at a location 0x80000000 away from the location at
3972 which it is linked. */
3974 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
3975 flag = bfd_reloc_overflow;
3977 case complain_overflow_unsigned:
3978 /* Checking for an unsigned overflow is relatively easy:
3979 trim the addresses and add, and trim the result as well.
3980 Overflow is normally indicated when the result does not
3981 fit in the field. However, we also need to consider the
3982 case when, e.g., fieldmask is 0x7fffffff or smaller, an
3983 input is 0x80000000, and bfd_vma is only 32 bits; then we
3984 will get sum == 0, but there is an overflow, since the
3985 inputs did not fit in the field. Instead of doing a
3986 separate test, we can check for this by or-ing in the
3987 operands when testing for the sum overflowing its final
3989 sum = (a + b) & addrmask;
3990 if ((a | b | sum) & signmask)
3991 flag = bfd_reloc_overflow;
3998 /* Put RELOCATION in the right bits. */
3999 relocation >>= (bfd_vma) rightshift;
4001 if ((howto->type == R_CKCORE_DOFFSET_LO16
4002 || howto->type == R_CKCORE_TOFFSET_LO16)
4004 /* Do nothing lsli32 rx, rz, 0. */
4008 /* Fir V1, all this relocation must be x -1. */
4009 if (howto->type == R_CKCORE_PCREL_IMM11BY2
4010 || howto->type == R_CKCORE_PCREL_JSR_IMM11BY2
4011 || howto->type == R_CKCORE_DOFFSET_LO16
4012 || howto->type == R_CKCORE_TOFFSET_LO16)
4014 else if (howto->type == R_CKCORE_PCREL_IMM7BY4)
4015 relocation = (relocation & 0x1f) + ((relocation << 3) & 0x300);
4016 else if (howto->type == R_CKCORE_PCREL_FLRW_IMM8BY4)
4018 = ((relocation << 4) & 0xf0) + ((relocation << 17) & 0x1e00000);
4019 else if (howto->type == R_CKCORE_NOJSRI)
4021 x = (x & howto->dst_mask) | CSKY_INSN_JSRI_TO_LRW;
4023 csky_put_insn_32 (input_bfd, CSKY_INSN_JSR_R26, location + 4);
4026 relocation <<= (bfd_vma) bitpos;
4027 /* Add RELOCATION to the right bits of X. */
4028 x = ((x & ~howto->dst_mask)
4029 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4031 /* Put the relocated value back in the object file. */
4037 bfd_put_8 (input_bfd, x, location);
4040 bfd_put_16 (input_bfd, x, location);
4043 if (need_reverse_bits)
4044 csky_put_insn_32 (input_bfd, x, location);
4046 bfd_put_32 (input_bfd, x, location);
4052 /* Look up an entry in the stub hash. Stub entries are cached because
4053 creating the stub name takes a bit of time. */
4055 static struct elf32_csky_stub_hash_entry *
4056 elf32_csky_get_stub_entry (const asection *input_section,
4057 const asection *sym_sec,
4058 struct elf_link_hash_entry *hash,
4059 const Elf_Internal_Rela *rel,
4060 struct csky_elf_link_hash_table *htab)
4062 struct elf32_csky_stub_hash_entry *stub_entry;
4063 struct csky_elf_link_hash_entry *h
4064 = (struct csky_elf_link_hash_entry *) hash;
4065 const asection *id_sec;
4067 if ((input_section->flags & SEC_CODE) == 0)
4070 /* If this input section is part of a group of sections sharing one
4071 stub section, then use the id of the first section in the group.
4072 Stub names need to include a section id, as there may well be
4073 more than one stub used to reach say, printf, and we need to
4074 distinguish between them. */
4075 id_sec = htab->stub_group[input_section->id].link_sec;
4076 if (h != NULL && h->stub_cache != NULL
4077 && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
4078 stub_entry = h->stub_cache;
4082 stub_name = elf32_csky_stub_name (id_sec, sym_sec, h, rel);
4083 if (stub_name == NULL)
4085 stub_entry = csky_stub_hash_lookup (&htab->stub_hash_table,
4086 stub_name, FALSE, FALSE);
4088 h->stub_cache = stub_entry;
4095 static bfd_reloc_status_type
4096 csky_final_link_relocate (reloc_howto_type *howto,
4098 asection *input_section,
4106 /* Sanity check the address. */
4107 if (address > bfd_get_section_limit (input_bfd, input_section))
4108 return bfd_reloc_outofrange;
4110 /* This function assumes that we are dealing with a basic relocation
4111 against a symbol. We want to compute the value of the symbol to
4112 relocate to. This is just VALUE, the value of the symbol,
4113 plus ADDEND, any addend associated with the reloc. */
4114 relocation = value + addend;
4116 /* If the relocation is PC relative, we want to set RELOCATION to
4117 the distance between the symbol (currently in RELOCATION) and the
4118 location we are relocating. Some targets (e.g., i386-aout)
4119 arrange for the contents of the section to be the negative of the
4120 offset of the location within the section; for such targets
4121 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
4122 simply leave the contents of the section as zero; for such
4123 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
4124 need to subtract out the offset of the location within the
4125 section (which is just ADDRESS). */
4126 if (howto->pc_relative)
4128 relocation -= (input_section->output_section->vma
4129 + input_section->output_offset);
4130 if (howto->pcrel_offset)
4131 relocation -= address;
4134 return csky_relocate_contents (howto, input_bfd, relocation,
4135 contents + address);
4139 /* Return the base VMA address which should be subtracted from real addresses
4140 when resolving @dtpoff relocation.
4141 This is PT_TLS segment p_vaddr. */
4144 dtpoff_base (struct bfd_link_info *info)
4146 /* If tls_sec is NULL, we should have signalled an error already. */
4147 if (elf_hash_table (info)->tls_sec == NULL)
4149 return elf_hash_table (info)->tls_sec->vma;
4152 /* Return the relocation value for @tpoff relocation
4153 if STT_TLS virtual address is ADDRESS. */
4156 tpoff (struct bfd_link_info *info, bfd_vma address)
4158 struct elf_link_hash_table *htab = elf_hash_table (info);
4161 /* If tls_sec is NULL, we should have signalled an error already. */
4162 if (htab->tls_sec == NULL)
4164 base = align_power ((bfd_vma) TCB_SIZE, htab->tls_sec->alignment_power);
4165 return address - htab->tls_sec->vma + base;
4168 /* Relocate a csky section. */
4171 csky_elf_relocate_section (bfd * output_bfd,
4172 struct bfd_link_info * info,
4174 asection * input_section,
4175 bfd_byte * contents,
4176 Elf_Internal_Rela * relocs,
4177 Elf_Internal_Sym * local_syms,
4178 asection ** local_sections)
4180 Elf_Internal_Shdr *symtab_hdr;
4181 struct elf_link_hash_entry **sym_hashes;
4182 Elf_Internal_Rela *rel;
4183 Elf_Internal_Rela *relend;
4185 bfd_boolean ret = TRUE;
4186 struct csky_elf_link_hash_table * htab;
4187 bfd_vma *local_got_offsets = elf_local_got_offsets (input_bfd);
4189 htab = csky_elf_hash_table (info);
4193 symtab_hdr = & elf_symtab_hdr (input_bfd);
4194 sym_hashes = elf_sym_hashes (input_bfd);
4197 relend = relocs + input_section->reloc_count;
4198 for (; rel < relend; rel++)
4200 enum elf_csky_reloc_type r_type
4201 = (enum elf_csky_reloc_type) ELF32_R_TYPE (rel->r_info);
4202 unsigned long r_symndx;
4203 reloc_howto_type * howto;
4204 Elf_Internal_Sym * sym;
4208 struct elf_link_hash_entry * h;
4209 bfd_vma addend = (bfd_vma)rel->r_addend;
4210 bfd_reloc_status_type r = bfd_reloc_ok;
4211 bfd_boolean unresolved_reloc = FALSE;
4212 int do_final_relocate = TRUE;
4213 bfd_boolean relative_reloc = FALSE;
4214 bfd_signed_vma disp;
4216 /* Ignore these relocation types:
4217 R_CKCORE_GNU_VTINHERIT, R_CKCORE_GNU_VTENTRY. */
4218 if (r_type == R_CKCORE_GNU_VTINHERIT || r_type == R_CKCORE_GNU_VTENTRY)
4221 if ((unsigned) r_type >= (unsigned) R_CKCORE_MAX)
4223 /* The r_type is error, not support it. */
4224 /* xgettext:c-format */
4225 _bfd_error_handler (_("%pB: unsupported relocation type: %#x"),
4227 bfd_set_error (bfd_error_bad_value);
4232 howto = &csky_elf_howto_table[(int) r_type];
4234 r_symndx = ELF32_R_SYM(rel->r_info);
4238 unresolved_reloc = FALSE;
4240 if (r_symndx < symtab_hdr->sh_info)
4242 /* Get symbol table entry. */
4243 sym = local_syms + r_symndx;
4244 sec = local_sections[r_symndx];
4245 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4246 addend = (bfd_vma)rel->r_addend;
4250 bfd_boolean warned, ignored;
4252 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4253 r_symndx, symtab_hdr, sym_hashes,
4255 unresolved_reloc, warned, ignored);
4258 if (sec != NULL && discarded_section (sec))
4260 /* For relocs against symbols from removed linkonce sections,
4261 or sections discarded by a linker script, we just want the
4262 section contents zeroed. Avoid any special processing.
4263 And if the symbol is referenced in '.csky_stack_size' section,
4264 set the address to SEC_DISCARDED(0xffffffff). */
4266 /* The .csky_stack_size section is just for callgraph. */
4267 if (strcmp (input_section->name, ".csky_stack_size") == 0)
4269 /* FIXME: it should define in head file. */
4270 #define SEC_DISCARDED 0xffffffff
4271 bfd_put_32 (input_bfd, SEC_DISCARDED, contents + rel->r_offset);
4278 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
4279 rel, 1, relend, howto, 0,
4283 if (bfd_link_relocatable (info))
4286 read_content_substitute = 0;
4290 + (bfd_signed_vma) addend
4291 - input_section->output_section->vma
4292 - input_section->output_offset
4294 /* It is for ck8xx. */
4295 #define CSKY_INSN_BSR32 0xe0000000
4296 /* It is for ck5xx/ck6xx. */
4297 #define CSKY_INSN_BSR16 0xf800
4298 #define within_range(x, L) (-(1 << (L - 1)) < (x) && (x) < (1 << (L -1)) - 2)
4299 switch (howto->type)
4301 case R_CKCORE_PCREL_IMM18BY2:
4302 /* When h is NULL, means the instruction written as
4304 if the highest bit is set, prevent the high 32bits
4305 turn to 0xffffffff when signed extern in 64bit
4307 if (h == NULL && (addend & 0x80000000))
4308 addend &= 0xffffffff;
4311 case R_CKCORE_PCREL32:
4314 case R_CKCORE_GOT12:
4315 case R_CKCORE_PLT12:
4316 case R_CKCORE_GOT_HI16:
4317 case R_CKCORE_GOT_LO16:
4318 case R_CKCORE_PLT_HI16:
4319 case R_CKCORE_PLT_LO16:
4320 case R_CKCORE_GOT32:
4321 case R_CKCORE_GOT_IMM18BY4:
4322 /* Relocation is to the entry for this symbol in the global
4324 BFD_ASSERT (htab->elf.sgot != NULL);
4327 /* Global symbol is defined by other modules. */
4329 off = h->got.offset;
4330 dyn = htab->elf.dynamic_sections_created;
4331 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4332 bfd_link_pic (info), h)
4333 || (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info,h))
4334 || (ELF_ST_VISIBILITY(h->other)
4335 && h->root.type == bfd_link_hash_undefweak))
4337 /* This is actually a static link, or it is a
4338 -Bsymbolic link and the symbol is defined
4339 locally, or the symbol was forced to be local
4340 because of a version file. We must initialize
4341 this entry in the global offset table. Since the
4342 offset must always be a multiple of 4, we use the
4343 least significant bit to record whether we have
4344 initialized it already.
4345 When doing a dynamic link, we create a .rela.dyn
4346 relocation entry to initialize the value. This
4347 is done in the finish_dynamic_symbol routine. FIXME */
4352 bfd_put_32 (output_bfd, relocation,
4353 htab->elf.sgot->contents + off);
4356 /* TRUE if relative relocation should be generated. GOT reference to
4357 global symbol in PIC will lead to dynamic symbol. It becomes a
4358 problem when "time" or "times" is defined as a variable in an
4359 executable, clashing with functions of the same name in libc. If a
4360 symbol isn't undefined weak symbol, don't make it dynamic in PIC and
4361 generate relative relocation. */
4362 #define GENERATE_RELATIVE_RELOC_P(INFO, H) \
4363 ((H)->dynindx == -1 \
4364 && !(H)->forced_local \
4365 && (H)->root.type != bfd_link_hash_undefweak \
4366 && bfd_link_pic (INFO))
4368 if (GENERATE_RELATIVE_RELOC_P (info, h))
4369 /* If this symbol isn't dynamic
4370 in PIC, generate R_CKCORE_RELATIVE here. */
4371 relative_reloc = TRUE;
4375 unresolved_reloc = FALSE;
4376 } /* End if h != NULL. */
4379 BFD_ASSERT (local_got_offsets != NULL);
4380 off = local_got_offsets[r_symndx];
4382 /* The offset must always be a multiple of 4. We use
4383 the least significant bit to record whether we have
4384 already generated the necessary reloc. */
4389 bfd_put_32 (output_bfd, relocation,
4390 htab->elf.sgot->contents + off);
4391 local_got_offsets[r_symndx] |= 1;
4392 if (bfd_link_pic (info))
4393 relative_reloc = TRUE;
4399 Elf_Internal_Rela outrel;
4402 srelgot = htab->elf.srelgot;
4403 BFD_ASSERT (srelgot != NULL);
4406 = (htab->elf.sgot->output_section->vma
4407 + htab->elf.sgot->output_offset + off);
4408 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4409 outrel.r_addend = relocation;
4410 loc = srelgot->contents;
4411 loc += (srelgot->reloc_count++ * sizeof (Elf32_External_Rela));
4413 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4415 relocation = htab->elf.sgot->output_offset + off;
4418 case R_CKCORE_GOTOFF_IMM18:
4419 case R_CKCORE_GOTOFF:
4420 case R_CKCORE_GOTOFF_HI16:
4421 case R_CKCORE_GOTOFF_LO16:
4422 /* Relocation is relative to the start of the global offset
4424 /* Note that sgot->output_offset is not involved in this
4425 calculation. We always want the start of .got. If we
4426 defined _GLOBAL_OFFSET_TABLE in a different way, as is
4427 permitted by the ABI, we might have to change this
4429 relocation -= htab->elf.sgot->output_section->vma;
4432 case R_CKCORE_GOTPC:
4433 case R_CKCORE_GOTPC_HI16:
4434 case R_CKCORE_GOTPC_LO16:
4435 /* Use global offset table as symbol value. */
4436 relocation = htab->elf.sgot->output_section->vma;
4438 unresolved_reloc = FALSE;
4441 case R_CKCORE_DOFFSET_IMM18:
4442 case R_CKCORE_DOFFSET_IMM18BY2:
4443 case R_CKCORE_DOFFSET_IMM18BY4:
4445 asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4446 relocation -= sdata->output_section->vma;
4450 case R_CKCORE_DOFFSET_LO16:
4452 asection *sdata = bfd_get_section_by_name (output_bfd, ".data");
4453 relocation -= sdata->output_section->vma;
4457 case R_CKCORE_TOFFSET_LO16:
4459 asection *stext = bfd_get_section_by_name (output_bfd, ".text");
4461 relocation -= stext->output_section->vma;
4465 case R_CKCORE_PLT_IMM18BY4:
4466 case R_CKCORE_PLT32:
4467 /* Relocation is to the entry for this symbol in the
4468 procedure linkage table. */
4470 /* Resolve a PLT32 reloc against a local symbol directly,
4471 without using the procedure linkage table. */
4475 if (h->plt.offset == (bfd_vma) -1 || htab->elf.splt == NULL)
4477 /* We didn't make a PLT entry for this symbol. This
4478 happens when statically linking PIC code, or when
4479 using -Bsymbolic. */
4480 if (h->got.offset != (bfd_vma) -1)
4484 off = h->got.offset;
4485 dyn = htab->elf.dynamic_sections_created;
4486 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4487 bfd_link_pic (info), h)
4488 || (bfd_link_pic (info)
4489 && SYMBOL_REFERENCES_LOCAL (info, h))
4490 || (ELF_ST_VISIBILITY (h->other)
4491 && h->root.type == bfd_link_hash_undefweak))
4493 /* This is actually a static link, or it is a
4494 -Bsymbolic link and the symbol is defined
4495 locally, or the symbol was forced to be local
4496 because of a version file. We must initialize
4497 this entry in the global offset table. Since the
4498 offset must always be a multiple of 4, we use the
4499 least significant bit to record whether we have
4500 initialized it already.
4502 When doing a dynamic link, we create a .rela.dyn
4503 relocation entry to initialize the value. This
4504 is done in the finish_dynamic_symbol routine.
4511 if (GENERATE_RELATIVE_RELOC_P (info, h))
4512 relative_reloc = TRUE;
4515 bfd_put_32 (output_bfd, relocation,
4516 htab->elf.sgot->contents + off);
4521 Elf_Internal_Rela outrel;
4524 srelgot = htab->elf.srelgot;
4525 BFD_ASSERT (srelgot != NULL);
4528 = (htab->elf.sgot->output_section->vma
4529 + htab->elf.sgot->output_offset + off);
4530 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4531 outrel.r_addend = relocation;
4532 loc = srelgot->contents;
4533 loc += (srelgot->reloc_count++
4534 * sizeof (Elf32_External_Rela));
4536 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4538 relocation = off + htab->elf.sgot->output_offset;
4542 /* The relocation is the got offset. */
4543 if (bfd_csky_abi (output_bfd) == CSKY_ABI_V2)
4544 relocation = (h->plt.offset / PLT_ENTRY_SIZE + 2) * 4;
4546 relocation = (h->plt.offset / PLT_ENTRY_SIZE_P + 2) * 4;
4547 unresolved_reloc = FALSE;
4550 case R_CKCORE_PCREL_IMM26BY2:
4551 case R_CKCORE_PCREL_JSR_IMM26BY2:
4552 case R_CKCORE_PCREL_JSR_IMM11BY2:
4553 case R_CKCORE_PCREL_IMM11BY2:
4554 case R_CKCORE_CALLGRAPH:
4555 /* Emit callgraph information first. */
4556 /* TODO: deal with callgraph. */
4557 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_CALLGRAPH)
4559 /* Some reloc need further handling. */
4560 /* h == NULL means the symbol is a local symbol,
4561 r_symndx == 0 means the symbol is 'ABS' and
4562 the relocation is already handled in assemble,
4563 here just use for callgraph. */
4564 /* TODO: deal with callgraph. */
4565 if (h == NULL && r_symndx == 0)
4567 do_final_relocate = FALSE;
4571 /* Ignore weak references to undefined symbols. */
4572 if (h != NULL && h->root.type == bfd_link_hash_undefweak)
4574 do_final_relocate = FALSE;
4578 /* Using branch stub. */
4579 if (use_branch_stub == TRUE
4580 && ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_IMM26BY2)
4582 struct elf32_csky_stub_hash_entry *stub_entry = NULL;
4583 if (sym_must_create_stub (h, info))
4584 stub_entry = elf32_csky_get_stub_entry (input_section,
4587 else if (disp > BSR_MAX_FWD_BRANCH_OFFSET
4588 || disp < BSR_MAX_BWD_BRANCH_OFFSET)
4589 stub_entry = elf32_csky_get_stub_entry (input_section,
4592 if (stub_entry != NULL)
4594 = (stub_entry->stub_offset
4595 + stub_entry->stub_sec->output_offset
4596 + stub_entry->stub_sec->output_section->vma);
4601 || (h->root.type == bfd_link_hash_defined
4602 && h->dynindx == -1)
4603 || ((h->def_regular && !h->def_dynamic)
4604 && (h->root.type != bfd_link_hash_defweak
4605 || ! bfd_link_pic (info))))
4607 if (ELF32_R_TYPE (rel->r_info) == R_CKCORE_PCREL_JSR_IMM26BY2)
4609 if (within_range (disp, 26))
4611 /* In range for BSR32. */
4612 howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM26BY2];
4613 read_content_substitute = CSKY_INSN_BSR32;
4615 else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810)
4616 /* if bsr32 cannot reach, generate
4617 "lrw r25, label; jsr r25" instead of
4619 howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4620 } /* if ELF32_R_TYPE (rel->r_info)... */
4621 else if (ELF32_R_TYPE (rel->r_info)
4622 == R_CKCORE_PCREL_JSR_IMM11BY2)
4624 if (within_range (disp, 11))
4626 /* In range for BSR16. */
4627 howto = &csky_elf_howto_table[R_CKCORE_PCREL_IMM11BY2];
4628 read_content_substitute = CSKY_INSN_BSR16;
4632 } /* else if h == NULL... */
4634 else if (bfd_csky_arch (output_bfd) == CSKY_ARCH_810
4635 && (ELF32_R_TYPE (rel->r_info)
4636 == R_CKCORE_PCREL_JSR_IMM26BY2))
4638 howto = &csky_elf_howto_table[R_CKCORE_NOJSRI];
4641 /* Other situation, h->def_dynamic == 1,
4642 undefined_symbol when output file is shared object, etc. */
4643 /* Else fall through. */
4645 case R_CKCORE_ADDR_HI16:
4646 case R_CKCORE_ADDR_LO16:
4647 if (bfd_link_pic (info)
4648 || (!bfd_link_pic (info)
4652 && ((h->def_dynamic && !h->def_regular)
4653 || (htab->elf.dynamic_sections_created
4654 && (h->root.type == bfd_link_hash_undefweak
4655 || h->root.type == bfd_link_hash_undefined
4656 || h->root.type == bfd_link_hash_indirect)))))
4658 Elf_Internal_Rela outrel;
4659 bfd_boolean skip, relocate;
4662 /* When generating a shared object, these relocations
4663 are copied into the output file to be resolved at
4669 _bfd_elf_section_offset (output_bfd, info, input_section,
4671 if (outrel.r_offset == (bfd_vma) -1)
4673 else if (outrel.r_offset == (bfd_vma) -2)
4678 outrel.r_offset += (input_section->output_section->vma
4679 + input_section->output_offset);
4681 memset (&outrel, 0, sizeof (outrel));
4684 && (!bfd_link_pic (info)
4685 || (!SYMBOLIC_BIND (info, h)
4686 && h->root.type == bfd_link_hash_defweak)
4687 || !h->def_regular))
4689 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4690 outrel.r_addend = rel->r_addend;
4694 /* This symbol is local, or marked to become local. */
4696 outrel.r_info = ELF32_R_INFO (0, r_type);
4697 outrel.r_addend = relocation + rel->r_addend;
4699 loc = htab->elf.srelgot->contents;
4700 loc += (htab->elf.srelgot->reloc_count++
4701 * sizeof (Elf32_External_Rela));
4704 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4706 /* If this reloc is against an external symbol, we do not
4707 want to diddle with the addend. Otherwise, we need to
4708 include the symbol value so that it becomes an addend
4709 for the dynamic reloc. */
4712 } /* if bfd_link_pic (info) ... */
4715 case R_CKCORE_ADDR32:
4716 /* r_symndx will be zero only for relocs against symbols
4717 from removed linkonce sections, or sections discarded
4719 This relocation don't nedd to handle, the value will
4720 be set to SEC_DISCARDED(0xffffffff). */
4722 && strcmp (sec->name, ".csky_stack_size") == 0)
4724 do_final_relocate = FALSE;
4727 if (r_symndx >= symtab_hdr->sh_info
4729 && bfd_link_executable (info))
4732 if (r_symndx == 0 || (input_section->flags & SEC_ALLOC) == 0)
4735 if (bfd_link_pic (info)
4738 && ((h->def_dynamic && !h->def_regular)
4739 || (htab->elf.dynamic_sections_created
4740 && (h->root.type == bfd_link_hash_undefweak
4741 || h->root.type == bfd_link_hash_undefined
4742 || h->root.type == bfd_link_hash_indirect)))))
4744 Elf_Internal_Rela outrel;
4745 bfd_boolean skip, relocate;
4748 /* When generating a shared object, these relocations
4749 are copied into the output file to be resolved at
4755 _bfd_elf_section_offset (output_bfd, info, input_section,
4758 if (outrel.r_offset == (bfd_vma) -1)
4760 else if (outrel.r_offset == (bfd_vma) -2)
4766 outrel.r_offset += (input_section->output_section->vma
4767 + input_section->output_offset);
4770 memset (&outrel, 0, sizeof (outrel));
4773 && (!bfd_link_pic (info)
4774 || (!SYMBOLIC_BIND (info, h)
4775 && h->root.type == bfd_link_hash_defweak)
4776 || !h->def_regular))
4778 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4779 outrel.r_addend = rel->r_addend;
4783 /* This symbol is local, or marked to become local. */
4784 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_RELATIVE);
4785 outrel.r_addend = relocation + rel->r_addend;
4788 loc = htab->elf.srelgot->contents;
4789 loc += (htab->elf.srelgot->reloc_count++
4790 * sizeof (Elf32_External_Rela));
4793 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4795 /* If this reloc is against an external symbol, we do
4796 want to diddle with the addend. Otherwise, we need to
4797 include the symbol value so that it becomes an addend
4798 for the dynamic reloc. */
4804 case R_CKCORE_TLS_LDO32:
4805 relocation = relocation - dtpoff_base (info);
4808 case R_CKCORE_TLS_LDM32:
4809 BFD_ASSERT (htab->elf.sgot != NULL);
4810 off = htab->tls_ldm_got.offset;
4815 /* If we don't know the module number,
4816 create a relocation for it. */
4817 if (!bfd_link_executable (info))
4819 Elf_Internal_Rela outrel;
4822 BFD_ASSERT (htab->elf.srelgot != NULL);
4823 outrel.r_addend = 0;
4825 = (htab->elf.sgot->output_section->vma
4826 + htab->elf.sgot->output_offset + off);
4827 outrel.r_info = ELF32_R_INFO (0, R_CKCORE_TLS_DTPMOD32);
4828 bfd_put_32 (output_bfd, outrel.r_addend,
4829 htab->elf.sgot->contents + off);
4831 loc = htab->elf.srelgot->contents;
4832 loc += (htab->elf.srelgot->reloc_count++
4833 * sizeof (Elf32_External_Rela));
4835 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4838 bfd_put_32 (output_bfd, 1,
4839 htab->elf.sgot->contents + off);
4840 htab->tls_ldm_got.offset |= 1;
4843 = (htab->elf.sgot->output_section->vma
4844 + htab->elf.sgot->output_offset + off
4845 - (input_section->output_section->vma
4846 + input_section->output_offset + rel->r_offset));
4848 case R_CKCORE_TLS_LE32:
4849 if (bfd_link_dll (info))
4852 /* xgettext:c-format */
4853 (_("%pB(%pA+%#" PRIx64 "): %s relocation not permitted "
4854 "in shared object"),
4855 input_bfd, input_section, (uint64_t)rel->r_offset,
4860 relocation = tpoff (info, relocation);
4862 case R_CKCORE_TLS_GD32:
4863 case R_CKCORE_TLS_IE32:
4868 BFD_ASSERT (htab->elf.sgot != NULL);
4874 dyn = htab->elf.dynamic_sections_created;
4875 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4876 bfd_link_pic (info), h)
4877 && (!bfd_link_pic (info)
4878 || !SYMBOL_REFERENCES_LOCAL (info, h)))
4880 unresolved_reloc = FALSE;
4883 off = h->got.offset;
4884 tls_type = ((struct csky_elf_link_hash_entry *)h)->tls_type;
4888 BFD_ASSERT (local_got_offsets != NULL);
4889 off = local_got_offsets[r_symndx];
4890 tls_type = csky_elf_local_got_tls_type (input_bfd)[r_symndx];
4893 BFD_ASSERT (tls_type != GOT_UNKNOWN);
4899 bfd_boolean need_relocs = FALSE;
4900 Elf_Internal_Rela outrel;
4901 bfd_byte *loc = NULL;
4903 /* The GOT entries have not been initialized yet. Do it
4904 now, and emit any relocations. If both an IE GOT and a
4905 GD GOT are necessary, we emit the GD first. */
4906 if ((!bfd_link_executable (info) || indx != 0)
4908 || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4909 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
4910 || h->root.type != bfd_link_hash_undefined))
4913 BFD_ASSERT (htab->elf.srelgot != NULL);
4915 loc = htab->elf.srelgot->contents;
4916 loc += (htab->elf.srelgot->reloc_count
4917 * sizeof (Elf32_External_Rela));
4919 if (tls_type & GOT_TLS_GD)
4923 outrel.r_addend = 0;
4925 = (htab->elf.sgot->output_section->vma
4926 + htab->elf.sgot->output_offset
4929 = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPMOD32);
4930 bfd_put_32 (output_bfd, outrel.r_addend,
4931 htab->elf.sgot->contents + cur_off);
4933 bfd_elf32_swap_reloca_out (output_bfd,
4935 loc += sizeof (Elf32_External_Rela);
4936 htab->elf.srelgot->reloc_count++;
4938 bfd_put_32 (output_bfd,
4939 relocation - dtpoff_base (info),
4940 (htab->elf.sgot->contents
4944 outrel.r_addend = 0;
4946 = ELF32_R_INFO (indx, R_CKCORE_TLS_DTPOFF32);
4947 outrel.r_offset += 4;
4948 bfd_put_32 (output_bfd, outrel.r_addend,
4949 (htab->elf.sgot->contents
4953 R_CKCORE_TLS_DTPOFF32);
4955 bfd_elf32_swap_reloca_out (output_bfd,
4958 htab->elf.srelgot->reloc_count++;
4959 loc += sizeof (Elf32_External_Rela);
4965 /* If are not emitting relocations for a
4966 general dynamic reference, then we must be in a
4967 static link or an executable link with the
4968 symbol binding locally. Mark it as belonging
4969 to module 1, the executable. */
4970 bfd_put_32 (output_bfd, 1,
4971 htab->elf.sgot->contents + cur_off);
4972 bfd_put_32 (output_bfd,
4973 relocation - dtpoff_base (info),
4974 htab->elf.sgot->contents
4979 if (tls_type & GOT_TLS_IE)
4984 outrel.r_addend = relocation - dtpoff_base (info);
4986 outrel.r_addend = 0;
4988 = (htab->elf.sgot->output_section->vma
4989 + htab->elf.sgot->output_offset + cur_off);
4991 = ELF32_R_INFO (indx, R_CKCORE_TLS_TPOFF32);
4993 bfd_put_32 (output_bfd, outrel.r_addend,
4994 htab->elf.sgot->contents + cur_off);
4996 bfd_elf32_swap_reloca_out (output_bfd,
4998 htab->elf.srelgot->reloc_count++;
4999 loc += sizeof (Elf32_External_Rela);
5002 bfd_put_32 (output_bfd, tpoff (info, relocation),
5003 htab->elf.sgot->contents + cur_off);
5008 local_got_offsets[r_symndx] |= 1;
5010 if ((tls_type & GOT_TLS_GD) && howto->type != R_CKCORE_TLS_GD32)
5013 = (htab->elf.sgot->output_section->vma
5014 + htab->elf.sgot->output_offset + off
5015 - (input_section->output_section->vma
5016 + input_section->output_offset
5021 /* No substitution when final linking. */
5022 read_content_substitute = 0;
5024 } /* End switch (howto->type). */
5026 /* Make sure 32-bit data in the text section will not be affected by
5027 our special endianness.
5028 However, this currently affects noting, since the ADDR32 howto type
5029 does no change with the data read. But we may need this mechanism in
5032 if (howto->size == 2
5033 && (howto->type == R_CKCORE_ADDR32
5034 || howto->type == R_CKCORE_PCREL32
5035 || howto->type == R_CKCORE_GOT32
5036 || howto->type == R_CKCORE_GOTOFF
5037 || howto->type == R_CKCORE_GOTPC
5038 || howto->type == R_CKCORE_PLT32
5039 || howto->type == R_CKCORE_TLS_LE32
5040 || howto->type == R_CKCORE_TLS_IE32
5041 || howto->type == R_CKCORE_TLS_LDM32
5042 || howto->type == R_CKCORE_TLS_GD32
5043 || howto->type == R_CKCORE_TLS_LDO32
5044 || howto->type == R_CKCORE_RELATIVE))
5045 need_reverse_bits = 0;
5047 need_reverse_bits = 1;
5048 /* Do the final link. */
5049 if (howto->type != R_CKCORE_PCREL_JSR_IMM11BY2
5050 && howto->type != R_CKCORE_PCREL_JSR_IMM26BY2
5051 && howto->type != R_CKCORE_CALLGRAPH
5052 && do_final_relocate)
5053 r = csky_final_link_relocate (howto, input_bfd, input_section,
5054 contents, rel->r_offset,
5055 relocation, addend);
5057 if (r != bfd_reloc_ok)
5064 case bfd_reloc_overflow:
5069 name = bfd_elf_string_from_elf_section (input_bfd,
5070 symtab_hdr->sh_link,
5075 name = bfd_section_name (input_bfd, sec);
5077 (*info->callbacks->reloc_overflow)
5079 (h ? &h->root : NULL),
5080 name, howto->name, (bfd_vma) 0,
5081 input_bfd, input_section, rel->r_offset);
5085 } /* End for (;rel < relend; rel++). */
5090 csky_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
5095 switch (note->descsz)
5099 /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
5101 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
5102 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5106 /* Sizeof (struct elf_prstatus) on C-SKY V1 arch. */
5108 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
5109 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
5114 /* Make a ".reg/999" section. */
5115 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
5116 size, note->descpos + offset);
5120 csky_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
5122 switch (note->descsz)
5127 /* Sizeof (struct elf_prpsinfo) on linux csky. */
5129 elf_tdata (abfd)->core->program
5130 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
5131 elf_tdata (abfd)->core->command
5132 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
5135 /* Note that for some reason, a spurious space is tacked
5136 onto the end of the args in some (at least one anyway)
5137 implementations, so strip it off if it exists. */
5139 char *command = elf_tdata (abfd)->core->command;
5140 int n = strlen (command);
5142 if (0 < n && command[n - 1] == ' ')
5143 command[n - 1] = '\0';
5149 /* End of external entry points for sizing and building linker stubs. */
5151 /* CPU-related basic API. */
5152 #define TARGET_BIG_SYM csky_elf32_be_vec
5153 #define TARGET_BIG_NAME "elf32-csky-big"
5154 #define TARGET_LITTLE_SYM csky_elf32_le_vec
5155 #define TARGET_LITTLE_NAME "elf32-csky-little"
5156 #define ELF_ARCH bfd_arch_csky
5157 #define ELF_MACHINE_CODE EM_CSKY
5158 #define ELF_MACHINE_ALT1 EM_CSKY_OLD
5159 #define ELF_MAXPAGESIZE 0x1000
5160 #define elf_info_to_howto csky_elf_info_to_howto
5161 #define elf_info_to_howto_rel NULL
5162 #define elf_backend_special_sections csky_elf_special_sections
5163 #define bfd_elf32_bfd_link_hash_table_create csky_elf_link_hash_table_create
5165 /* Target related API. */
5166 #define bfd_elf32_mkobject csky_elf_mkobject
5167 #define bfd_elf32_bfd_merge_private_bfd_data csky_elf_merge_private_bfd_data
5168 #define bfd_elf32_bfd_set_private_flags csky_elf_set_private_flags
5169 #define elf_backend_copy_indirect_symbol csky_elf_copy_indirect_symbol
5171 /* GC section related API. */
5172 #define elf_backend_can_gc_sections 1
5173 #define elf_backend_gc_mark_hook csky_elf_gc_mark_hook
5174 #define elf_backend_gc_mark_extra_sections elf32_csky_gc_mark_extra_sections
5176 /* Relocation related API. */
5177 #define elf_backend_reloc_type_class csky_elf_reloc_type_class
5178 #define bfd_elf32_bfd_reloc_type_lookup csky_elf_reloc_type_lookup
5179 #define bfd_elf32_bfd_reloc_name_lookup csky_elf_reloc_name_lookup
5180 #define elf_backend_ignore_discarded_relocs csky_elf_ignore_discarded_relocs
5181 #define elf_backend_relocate_section csky_elf_relocate_section
5182 #define elf_backend_check_relocs csky_elf_check_relocs
5184 /* Dynamic relocate related API. */
5185 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
5186 #define elf_backend_adjust_dynamic_symbol csky_elf_adjust_dynamic_symbol
5187 #define elf_backend_size_dynamic_sections csky_elf_size_dynamic_sections
5188 #define elf_backend_finish_dynamic_symbol csky_elf_finish_dynamic_symbol
5189 #define elf_backend_finish_dynamic_sections csky_elf_finish_dynamic_sections
5190 #define elf_backend_rela_normal 1
5191 #define elf_backend_can_refcount 1
5192 #define elf_backend_plt_readonly 1
5193 #define elf_backend_want_got_sym 1
5194 #define elf_backend_want_dynrelro 1
5195 #define elf_backend_got_header_size 12
5196 #define elf_backend_want_got_plt 1
5198 /* C-SKY coredump support. */
5199 #define elf_backend_grok_prstatus csky_elf_grok_prstatus
5200 #define elf_backend_grok_psinfo csky_elf_grok_psinfo
5202 #include "elf32-target.h"