1 /* ft32-specific support for 32-bit ELF.
2 Copyright (C) 2013-2018 Free Software Foundation, Inc.
4 Copied from elf32-moxie.c which is..
5 Copyright (C) 2009-2018 Free Software Foundation, Inc.
6 Free Software Foundation, Inc.
8 This file is part of BFD, the Binary File Descriptor library.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
23 MA 02110-1301, USA. */
30 #include "opcode/ft32.h"
32 static bfd_boolean debug_relax = FALSE;
34 static bfd_reloc_status_type
35 bfd_elf_ft32_diff_reloc (bfd *, arelent *, asymbol *, void *,
36 asection *, bfd *, char **);
38 static reloc_howto_type ft32_elf_howto_table [] =
40 /* This reloc does nothing. */
41 HOWTO (R_FT32_NONE, /* type */
43 2, /* size (0 = byte, 1 = short, 2 = long) */
45 FALSE, /* pc_relative */
47 complain_overflow_bitfield, /* complain_on_overflow */
48 bfd_elf_generic_reloc, /* special_function */
49 "R_FT32_NONE", /* name */
50 FALSE, /* partial_inplace */
53 FALSE), /* pcrel_offset */
55 /* A 32 bit absolute relocation. */
57 HOWTO (R_FT32_32, /* type */
59 2, /* size (0 = byte, 1 = short, 2 = long) */
61 FALSE, /* pc_relative */
63 complain_overflow_bitfield, /* complain_on_overflow */
64 bfd_elf_generic_reloc, /* special_function */
65 "R_FT32_32", /* name */
66 FALSE, /* partial_inplace */
67 0x00000000, /* src_mask */
68 0xffffffff, /* dst_mask */
69 FALSE), /* pcrel_offset */
71 HOWTO (R_FT32_16, /* type */
73 1, /* size (0 = byte, 1 = short, 2 = long) */
75 FALSE, /* pc_relative */
77 complain_overflow_dont, /* complain_on_overflow */
78 bfd_elf_generic_reloc, /* special_function */
79 "R_FT32_16", /* name */
80 FALSE, /* partial_inplace */
81 0x00000000, /* src_mask */
82 0x0000ffff, /* dst_mask */
83 FALSE), /* pcrel_offset */
85 HOWTO (R_FT32_8, /* type */
87 0, /* size (0 = byte, 1 = short, 2 = long) */
89 FALSE, /* pc_relative */
91 complain_overflow_signed, /* complain_on_overflow */
92 bfd_elf_generic_reloc, /* special_function */
93 "R_FT32_8", /* name */
94 FALSE, /* partial_inplace */
95 0x00000000, /* src_mask */
96 0x000000ff, /* dst_mask */
97 FALSE), /* pcrel_offset */
99 HOWTO (R_FT32_10, /* type */
101 1, /* size (0 = byte, 1 = short, 2 = long) */
103 FALSE, /* pc_relative */
105 complain_overflow_bitfield, /* complain_on_overflow */
106 bfd_elf_generic_reloc, /* special_function */
107 "R_FT32_10", /* name */
108 FALSE, /* partial_inplace */
109 0x00000000, /* src_mask */
110 0x00003ff0, /* dst_mask */
111 FALSE), /* pcrel_offset */
113 HOWTO (R_FT32_20, /* type */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
117 FALSE, /* pc_relative */
119 complain_overflow_dont, /* complain_on_overflow */
120 bfd_elf_generic_reloc, /* special_function */
121 "R_FT32_20", /* name */
122 FALSE, /* partial_inplace */
123 0x00000000, /* src_mask */
124 0x000fffff, /* dst_mask */
125 FALSE), /* pcrel_offset */
127 HOWTO (R_FT32_17, /* type */
129 2, /* size (0 = byte, 1 = short, 2 = long) */
131 FALSE, /* pc_relative */
133 complain_overflow_dont, /* complain_on_overflow */
134 bfd_elf_generic_reloc, /* special_function */
135 "R_FT32_17", /* name */
136 FALSE, /* partial_inplace */
137 0x00000000, /* src_mask */
138 0x0001ffff, /* dst_mask */
139 FALSE), /* pcrel_offset */
141 HOWTO (R_FT32_18, /* type */
143 2, /* size (0 = byte, 1 = short, 2 = long) */
145 FALSE, /* pc_relative */
147 complain_overflow_dont, /* complain_on_overflow */
148 bfd_elf_generic_reloc, /* special_function */
149 "R_FT32_18", /* name */
150 FALSE, /* partial_inplace */
151 0x00000000, /* src_mask */
152 0x0003ffff, /* dst_mask */
153 FALSE), /* pcrel_offset */
155 HOWTO (R_FT32_RELAX, /* type */
157 1, /* size (0 = byte, 1 = short, 2 = long) */
159 FALSE, /* pc_relative */
161 complain_overflow_signed, /* complain_on_overflow */
162 bfd_elf_generic_reloc, /* special_function */
163 "R_FT32_RELAX", /* name */
164 FALSE, /* partial_inplace */
165 0x00000000, /* src_mask */
166 0x00000000, /* dst_mask */
167 FALSE), /* pcrel_offset */
169 HOWTO (R_FT32_SC0, /* type */
171 1, /* size (0 = byte, 1 = short, 2 = long) */
173 FALSE, /* pc_relative */
175 complain_overflow_signed, /* complain_on_overflow */
176 bfd_elf_generic_reloc, /* special_function */
177 "R_FT32_SC0", /* name */
178 FALSE, /* partial_inplace */
179 0x00000000, /* src_mask */
180 0x00000000, /* dst_mask */
181 FALSE), /* pcrel_offset */
182 HOWTO (R_FT32_SC1, /* type */
184 2, /* size (0 = byte, 1 = short, 2 = long) */
186 TRUE, /* pc_relative */
188 complain_overflow_dont, /* complain_on_overflow */
189 bfd_elf_generic_reloc, /* special_function */
190 "R_FT32_SC1", /* name */
191 TRUE, /* partial_inplace */
192 0x07ffff80, /* src_mask */
193 0x07ffff80, /* dst_mask */
194 FALSE), /* pcrel_offset */
195 HOWTO (R_FT32_15, /* type */
197 2, /* size (0 = byte, 1 = short, 2 = long) */
199 FALSE, /* pc_relative */
201 complain_overflow_dont, /* complain_on_overflow */
202 bfd_elf_generic_reloc, /* special_function */
203 "R_FT32_15", /* name */
204 FALSE, /* partial_inplace */
205 0x00000000, /* src_mask */
206 0x00007fff, /* dst_mask */
207 FALSE), /* pcrel_offset */
208 HOWTO (R_FT32_DIFF32, /* type */
210 2, /* size (0 = byte, 1 = short, 2 = long) */
212 FALSE, /* pc_relative */
214 complain_overflow_dont, /* complain_on_overflow */
215 bfd_elf_ft32_diff_reloc, /* special_function */
216 "R_FT32_DIFF32", /* name */
217 FALSE, /* partial_inplace */
219 0xffffffff, /* dst_mask */
220 FALSE), /* pcrel_offset */
224 /* Map BFD reloc types to FT32 ELF reloc types. */
226 struct ft32_reloc_map
228 bfd_reloc_code_real_type bfd_reloc_val;
229 unsigned int ft32_reloc_val;
232 static const struct ft32_reloc_map ft32_reloc_map [] =
234 { BFD_RELOC_NONE, R_FT32_NONE },
235 { BFD_RELOC_32, R_FT32_32 },
236 { BFD_RELOC_16, R_FT32_16 },
237 { BFD_RELOC_8, R_FT32_8 },
238 { BFD_RELOC_FT32_10, R_FT32_10 },
239 { BFD_RELOC_FT32_20, R_FT32_20 },
240 { BFD_RELOC_FT32_17, R_FT32_17 },
241 { BFD_RELOC_FT32_18, R_FT32_18 },
242 { BFD_RELOC_FT32_RELAX, R_FT32_RELAX },
243 { BFD_RELOC_FT32_SC0, R_FT32_SC0 },
244 { BFD_RELOC_FT32_SC1, R_FT32_SC1 },
245 { BFD_RELOC_FT32_15, R_FT32_15 },
246 { BFD_RELOC_FT32_DIFF32, R_FT32_DIFF32 },
249 /* Perform a diff relocation. Nothing to do, as the difference value is
250 already written into the section's contents. */
252 static bfd_reloc_status_type
253 bfd_elf_ft32_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
254 arelent *reloc_entry ATTRIBUTE_UNUSED,
255 asymbol *symbol ATTRIBUTE_UNUSED,
256 void *data ATTRIBUTE_UNUSED,
257 asection *input_section ATTRIBUTE_UNUSED,
258 bfd *output_bfd ATTRIBUTE_UNUSED,
259 char **error_message ATTRIBUTE_UNUSED)
264 static reloc_howto_type *
265 ft32_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
266 bfd_reloc_code_real_type code)
270 for (i = sizeof (ft32_reloc_map) / sizeof (ft32_reloc_map[0]);
272 if (ft32_reloc_map [i].bfd_reloc_val == code)
273 return & ft32_elf_howto_table [ft32_reloc_map[i].ft32_reloc_val];
278 static reloc_howto_type *
279 ft32_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
284 i < sizeof (ft32_elf_howto_table) / sizeof (ft32_elf_howto_table[0]);
286 if (ft32_elf_howto_table[i].name != NULL
287 && strcasecmp (ft32_elf_howto_table[i].name, r_name) == 0)
288 return &ft32_elf_howto_table[i];
293 /* Set the howto pointer for an FT32 ELF reloc. */
296 ft32_info_to_howto_rela (bfd *abfd,
298 Elf_Internal_Rela *dst)
302 r_type = ELF32_R_TYPE (dst->r_info);
303 if (r_type >= (unsigned int) R_FT32_max)
305 /* xgettext:c-format */
306 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
308 bfd_set_error (bfd_error_bad_value);
312 cache_ptr->howto = & ft32_elf_howto_table [r_type];
313 return cache_ptr->howto != NULL;
316 /* Relocate an FT32 ELF section.
318 The RELOCATE_SECTION function is called by the new ELF backend linker
319 to handle the relocations for a section.
321 The relocs are always passed as Rela structures; if the section
322 actually uses Rel structures, the r_addend field will always be
325 This function is responsible for adjusting the section contents as
326 necessary, and (if using Rela relocs and generating a relocatable
327 output file) adjusting the reloc addend as necessary.
329 This function does not have to worry about setting the reloc
330 address or the reloc symbol index.
332 LOCAL_SYMS is a pointer to the swapped in local symbols.
334 LOCAL_SECTIONS is an array giving the section in the input file
335 corresponding to the st_shndx field of each local symbol.
337 The global hash table entry for the global symbols can be found
338 via elf_sym_hashes (input_bfd).
340 When generating relocatable output, this function must handle
341 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
342 going to be the section symbol corresponding to the output
343 section, which means that the addend must be adjusted
347 ft32_elf_relocate_section (bfd *output_bfd,
348 struct bfd_link_info *info,
350 asection *input_section,
352 Elf_Internal_Rela *relocs,
353 Elf_Internal_Sym *local_syms,
354 asection **local_sections)
356 Elf_Internal_Shdr *symtab_hdr;
357 struct elf_link_hash_entry **sym_hashes;
358 Elf_Internal_Rela *rel;
359 Elf_Internal_Rela *relend;
361 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
362 sym_hashes = elf_sym_hashes (input_bfd);
363 relend = relocs + input_section->reloc_count;
365 for (rel = relocs; rel < relend; rel ++)
367 reloc_howto_type *howto;
368 unsigned long r_symndx;
369 Elf_Internal_Sym *sym;
371 struct elf_link_hash_entry *h;
373 bfd_reloc_status_type r;
377 r_type = ELF32_R_TYPE (rel->r_info);
378 r_symndx = ELF32_R_SYM (rel->r_info);
379 howto = ft32_elf_howto_table + r_type;
384 if (r_symndx < symtab_hdr->sh_info)
386 sym = local_syms + r_symndx;
387 sec = local_sections [r_symndx];
388 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
390 name = bfd_elf_string_from_elf_section
391 (input_bfd, symtab_hdr->sh_link, sym->st_name);
392 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
396 bfd_boolean unresolved_reloc, warned, ignored;
398 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
399 r_symndx, symtab_hdr, sym_hashes,
401 unresolved_reloc, warned, ignored);
403 name = h->root.root.string;
406 if (sec != NULL && discarded_section (sec))
407 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
408 rel, 1, relend, howto, 0, contents);
410 if (bfd_link_relocatable (info))
419 unsigned int code15[2];
421 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
422 ft32_split_shortcode (insn, code15);
424 offset = (int)relocation;
425 offset += (int)(rel->r_addend - rel->r_offset);
426 offset -= (input_section->output_section->vma +
427 input_section->output_offset);
428 if ((offset < -1024) || (offset >= 1024))
430 r = bfd_reloc_outofrange;
433 code15[0] |= ((offset / 4) & 511);
434 insn = ft32_merge_shortcode (code15);
435 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
444 unsigned int code15[2];
446 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
447 ft32_split_shortcode (insn, code15);
449 offset = (int)relocation;
450 offset += (int)(rel->r_addend - rel->r_offset);
451 offset -= (input_section->output_section->vma +
452 input_section->output_offset);
453 if ((offset < -1024) || (offset >= 1024))
455 r = bfd_reloc_outofrange;
458 code15[1] |= ((offset / 4) & 511);
459 insn = ft32_merge_shortcode (code15);
460 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
470 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
471 contents, rel->r_offset,
472 relocation, rel->r_addend);
476 if (r != bfd_reloc_ok)
478 const char * msg = NULL;
482 case bfd_reloc_overflow:
483 (*info->callbacks->reloc_overflow)
484 (info, (h ? &h->root : NULL), name, howto->name,
485 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
488 case bfd_reloc_undefined:
489 (*info->callbacks->undefined_symbol)
490 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
493 case bfd_reloc_outofrange:
494 msg = _("internal error: out of range error");
497 case bfd_reloc_notsupported:
498 msg = _("internal error: unsupported relocation error");
501 case bfd_reloc_dangerous:
502 msg = _("internal error: dangerous relocation");
506 msg = _("internal error: unknown error");
511 (*info->callbacks->warning) (info, msg, name, input_bfd,
512 input_section, rel->r_offset);
525 Elf_Internal_Sym * isymbuf ATTRIBUTE_UNUSED,
527 bfd_vma pc ATTRIBUTE_UNUSED,
528 Elf_Internal_Rela * irel,
531 Elf_Internal_Shdr *symtab_hdr ATTRIBUTE_UNUSED;
534 enum elf_ft32_reloc_type r_type;
535 reloc_howto_type *howto = NULL;
540 r_type = ELF32_R_TYPE (irel->r_info);
541 howto = &ft32_elf_howto_table [r_type];
543 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
545 /* Get the value of the symbol referred to by the reloc. */
546 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
548 /* A local symbol. */
549 Elf_Internal_Sym *isym;
552 isym = isymbuf + ELF32_R_SYM (irel->r_info);
553 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
554 symval = isym->st_value;
555 /* If the reloc is absolute, it will not have
556 a symbol or section associated with it. */
558 symval += sym_sec->output_section->vma
559 + sym_sec->output_offset;
564 struct elf_link_hash_entry *h;
566 /* An external symbol. */
567 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
568 h = elf_sym_hashes (abfd)[indx];
569 BFD_ASSERT (h != NULL);
570 if (h->root.type != bfd_link_hash_defined
571 && h->root.type != bfd_link_hash_defweak)
572 /* This appears to be a reference to an undefined
573 symbol. Just ignore it--it will be caught by the
574 regular reloc processing. */
577 symval = (h->root.u.def.value
578 + h->root.u.def.section->output_section->vma
579 + h->root.u.def.section->output_offset);
591 insn = bfd_get_32 (abfd, contents + irel->r_offset);
592 insn |= ((symval + irel->r_addend) << howto->bitpos) & howto->dst_mask;
593 return ft32_shortcode (insn, sc);
596 insn = bfd_get_32 (abfd, contents + irel->r_offset);
597 /* Get the address of this instruction. */
598 dot = (sec->output_section->vma
599 + sec->output_offset + irel->r_offset);
600 value = symval + irel->r_addend;
601 offset = (value - dot) / 4;
603 if ((dot > 0x8c) && (-256 <= offset) && (offset < 256))
607 case 0x00200000: *sc = (3 << 13) | (0 << 9); return TRUE;
608 case 0x00280000: *sc = (3 << 13) | (1 << 9); return TRUE;
609 case 0x00600000: *sc = (3 << 13) | (2 << 9); return TRUE;
610 case 0x00680000: *sc = (3 << 13) | (3 << 9); return TRUE;
611 case 0x00a00000: *sc = (3 << 13) | (4 << 9); return TRUE;
612 case 0x00a80000: *sc = (3 << 13) | (5 << 9); return TRUE;
613 case 0x00e00000: *sc = (3 << 13) | (6 << 9); return TRUE;
614 case 0x00e80000: *sc = (3 << 13) | (7 << 9); return TRUE;
615 case 0x01200000: *sc = (3 << 13) | (8 << 9); return TRUE;
616 case 0x01280000: *sc = (3 << 13) | (9 << 9); return TRUE;
617 case 0x01600000: *sc = (3 << 13) | (10 << 9); return TRUE;
618 case 0x01680000: *sc = (3 << 13) | (11 << 9); return TRUE;
619 case 0x01a00000: *sc = (3 << 13) | (12 << 9); return TRUE;
620 case 0x01a80000: *sc = (3 << 13) | (13 << 9); return TRUE;
622 case 0x00300000: *sc = (3 << 13) | (14 << 9); return TRUE;
623 case 0x00340000: *sc = (3 << 13) | (15 << 9); return TRUE;
637 /* Returns whether the relocation type passed is a diff reloc. */
640 elf32_ft32_is_diff_reloc (Elf_Internal_Rela *irel)
642 return (ELF32_R_TYPE (irel->r_info) == R_FT32_DIFF32);
645 /* Reduce the diff value written in the section by count if the shrinked
646 insn address happens to fall between the two symbols for which this
647 diff reloc was emitted. */
650 elf32_ft32_adjust_diff_reloc_value (bfd *abfd,
651 struct bfd_section *isec,
652 Elf_Internal_Rela *irel,
654 bfd_vma shrinked_insn_address,
657 unsigned char * reloc_contents = NULL;
658 unsigned char * isec_contents = elf_section_data (isec)->this_hdr.contents;
659 bfd_signed_vma x = 0;
660 bfd_vma sym2_address;
661 bfd_vma sym1_address;
662 bfd_vma start_address;
666 if (isec_contents == NULL)
668 if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
671 elf_section_data (isec)->this_hdr.contents = isec_contents;
674 reloc_contents = isec_contents + irel->r_offset;
676 /* Read value written in object file. */
677 switch (ELF32_R_TYPE (irel->r_info))
680 x = bfd_get_signed_32 (abfd, reloc_contents);
687 /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
688 into the object file at the reloc offset. sym2's logical value is
689 symval (<start_of_section>) + reloc addend. Compute the start and end
690 addresses and check if the shrinked insn falls between sym1 and sym2. */
691 sym2_address = symval + irel->r_addend;
692 sym1_address = sym2_address - x;
694 /* Don't assume sym2 is bigger than sym1 - the difference
695 could be negative. Compute start and end addresses, and
696 use those to see if they span shrinked_insn_address. */
697 start_address = sym1_address < sym2_address ? sym1_address : sym2_address;
698 end_address = sym1_address > sym2_address ? sym1_address : sym2_address;
700 if (shrinked_insn_address >= start_address
701 && shrinked_insn_address < end_address)
703 /* Reduce the diff value by count bytes and write it back into section
705 bfd_signed_vma new_diff = x < 0 ? x + count : x - count;
707 if (sym2_address > shrinked_insn_address)
708 irel->r_addend -= count;
710 switch (ELF32_R_TYPE (irel->r_info))
713 bfd_put_signed_32 (abfd, new_diff & 0xFFFFFFFF, reloc_contents);
725 elf32_ft32_adjust_reloc_if_spans_insn (bfd *abfd,
727 Elf_Internal_Rela *irel, bfd_vma symval,
728 bfd_vma shrinked_insn_address,
729 bfd_vma shrink_boundary,
733 if (elf32_ft32_is_diff_reloc (irel))
735 if (!elf32_ft32_adjust_diff_reloc_value (abfd, isec, irel,
737 shrinked_insn_address,
743 bfd_vma reloc_value = symval + irel->r_addend;
744 bfd_boolean addend_within_shrink_boundary =
745 (reloc_value <= shrink_boundary);
746 bfd_boolean reloc_spans_insn =
747 (symval <= shrinked_insn_address
748 && reloc_value > shrinked_insn_address
749 && addend_within_shrink_boundary);
751 if (! reloc_spans_insn)
754 irel->r_addend -= count;
757 printf ("Relocation's addend needed to be fixed \n");
762 /* Delete some bytes from a section while relaxing. */
765 elf32_ft32_relax_delete_bytes (struct bfd_link_info *link_info, bfd * abfd,
766 asection * sec, bfd_vma addr, int count)
768 Elf_Internal_Shdr *symtab_hdr;
769 unsigned int sec_shndx;
771 Elf_Internal_Rela *irel, *irelend;
773 Elf_Internal_Sym *isym;
774 Elf_Internal_Sym *isymend;
775 struct elf_link_hash_entry **sym_hashes;
776 struct elf_link_hash_entry **end_hashes;
777 struct elf_link_hash_entry **start_hashes;
778 unsigned int symcount;
779 Elf_Internal_Sym *isymbuf = NULL;
781 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
782 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
784 contents = elf_section_data (sec)->this_hdr.contents;
788 irel = elf_section_data (sec)->relocs;
789 irelend = irel + sec->reloc_count;
791 /* Actually delete the bytes. */
792 memmove (contents + addr, contents + addr + count,
793 (size_t) (toaddr - addr - count));
796 /* Adjust all the relocs. */
797 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
798 /* Get the new reloc address. */
799 if ((irel->r_offset > addr && irel->r_offset < toaddr))
800 irel->r_offset -= count;
802 /* The reloc's own addresses are now ok. However, we need to readjust
803 the reloc's addend, i.e. the reloc's value if two conditions are met:
804 1.) the reloc is relative to a symbol in this section that
805 is located in front of the shrinked instruction
806 2.) symbol plus addend end up behind the shrinked instruction.
808 The most common case where this happens are relocs relative to
809 the section-start symbol.
811 This step needs to be done for all of the sections of the bfd. */
813 struct bfd_section *isec;
815 for (isec = abfd->sections; isec; isec = isec->next)
818 bfd_vma shrinked_insn_address;
820 if (isec->reloc_count == 0)
823 shrinked_insn_address = (sec->output_section->vma
824 + sec->output_offset + addr - count);
826 irel = elf_section_data (isec)->relocs;
827 /* PR 12161: Read in the relocs for this section if necessary. */
829 irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
831 for (irelend = irel + isec->reloc_count; irel < irelend; irel++)
833 /* Read this BFD's local symbols if we haven't done
835 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
837 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
839 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
840 symtab_hdr->sh_info, 0,
846 /* Get the value of the symbol referred to by the reloc. */
847 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
849 /* A local symbol. */
852 isym = isymbuf + ELF32_R_SYM (irel->r_info);
853 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
854 symval = isym->st_value;
855 /* If the reloc is absolute, it will not have
856 a symbol or section associated with it. */
859 symval += sym_sec->output_section->vma
860 + sym_sec->output_offset;
863 printf ("Checking if the relocation's "
864 "addend needs corrections.\n"
865 "Address of anchor symbol: 0x%x \n"
866 "Address of relocation target: 0x%x \n"
867 "Address of relaxed insn: 0x%x \n",
868 (unsigned int) symval,
869 (unsigned int) (symval + irel->r_addend),
870 (unsigned int) shrinked_insn_address);
872 if (symval <= shrinked_insn_address
873 && (symval + irel->r_addend) > shrinked_insn_address)
875 /* If there is an alignment boundary, we only need to
876 adjust addends that end up below the boundary. */
877 bfd_vma shrink_boundary = (toaddr
878 + sec->output_section->vma
879 + sec->output_offset);
883 ("Relocation's addend needed to be fixed \n");
885 if (!elf32_ft32_adjust_reloc_if_spans_insn (abfd, isec,
887 shrinked_insn_address,
893 /* else reference symbol is absolute. No adjustment needed. */
895 /* else...Reference symbol is extern. No need for adjusting
901 /* Adjust the local symbols defined in this section. */
902 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
903 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
906 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
908 if (isym->st_shndx == sec_shndx
909 && isym->st_value > addr && isym->st_value < toaddr)
910 isym->st_value -= count;
914 /* Now adjust the global symbols defined in this section. */
915 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
916 - symtab_hdr->sh_info);
917 sym_hashes = start_hashes = elf_sym_hashes (abfd);
918 end_hashes = sym_hashes + symcount;
920 for (; sym_hashes < end_hashes; sym_hashes++)
922 struct elf_link_hash_entry *sym_hash = *sym_hashes;
924 /* The '--wrap SYMBOL' option is causing a pain when the object file,
925 containing the definition of __wrap_SYMBOL, includes a direct
926 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
927 the same symbol (which is __wrap_SYMBOL), but still exist as two
928 different symbols in 'sym_hashes', we don't want to adjust
929 the global symbol __wrap_SYMBOL twice.
930 This check is only relevant when symbols are being wrapped. */
931 if (link_info->wrap_hash != NULL)
933 struct elf_link_hash_entry **cur_sym_hashes;
935 /* Loop only over the symbols whom been already checked. */
936 for (cur_sym_hashes = start_hashes; cur_sym_hashes < sym_hashes;
938 /* If the current symbol is identical to 'sym_hash', that means
939 the symbol was already adjusted (or at least checked). */
940 if (*cur_sym_hashes == sym_hash)
943 /* Don't adjust the symbol again. */
944 if (cur_sym_hashes < sym_hashes)
948 if ((sym_hash->root.type == bfd_link_hash_defined
949 || sym_hash->root.type == bfd_link_hash_defweak)
950 && sym_hash->root.u.def.section == sec
951 && sym_hash->root.u.def.value > addr
952 && sym_hash->root.u.def.value < toaddr)
953 sym_hash->root.u.def.value -= count;
959 /* Return TRUE if LOC can be a target of a branch, jump or call. */
962 elf32_ft32_relax_is_branch_target (struct bfd_link_info *link_info,
963 bfd * abfd, asection * sec,
966 Elf_Internal_Shdr *symtab_hdr;
967 Elf_Internal_Rela *irel, *irelend;
968 Elf_Internal_Sym *isym;
969 Elf_Internal_Sym *isymbuf = NULL;
971 struct bfd_section *isec;
973 struct elf_link_hash_entry **sym_hashes;
974 struct elf_link_hash_entry **end_hashes;
975 struct elf_link_hash_entry **start_hashes;
976 unsigned int symcount;
978 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
980 /* Now we check for relocations pointing to ret. */
981 for (isec = abfd->sections; isec; isec = isec->next)
983 irel = elf_section_data (isec)->relocs;
985 irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
987 irelend = irel + isec->reloc_count;
989 for (; irel < irelend; irel++)
991 /* Read this BFD's local symbols if we haven't done
993 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
995 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
997 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
998 symtab_hdr->sh_info, 0,
1000 if (isymbuf == NULL)
1004 /* Get the value of the symbol referred to by the reloc. */
1005 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1007 /* A local symbol. */
1010 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1011 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1012 symval = isym->st_value;
1013 /* If the reloc is absolute, it will not have
1014 a symbol or section associated with it. */
1017 symval += sym_sec->output_section->vma
1018 + sym_sec->output_offset;
1021 printf ("0x%x: Address of anchor symbol: 0x%x "
1022 "Address of relocation target: 0x%x \n",
1023 (unsigned int) irel->r_offset,
1024 (unsigned int) symval,
1025 (unsigned int) (symval + irel->r_addend));
1026 if ((irel->r_addend) == loc)
1033 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1034 - symtab_hdr->sh_info);
1035 sym_hashes = start_hashes = elf_sym_hashes (abfd);
1036 end_hashes = sym_hashes + symcount;
1038 for (; sym_hashes < end_hashes; sym_hashes++)
1040 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1042 /* The '--wrap SYMBOL' option is causing a pain when the object file,
1043 containing the definition of __wrap_SYMBOL, includes a direct
1044 call to SYMBOL as well. Since both __wrap_SYMBOL and SYMBOL reference
1045 the same symbol (which is __wrap_SYMBOL), but still exist as two
1046 different symbols in 'sym_hashes', we don't want to adjust
1047 the global symbol __wrap_SYMBOL twice.
1048 This check is only relevant when symbols are being wrapped. */
1049 if (link_info->wrap_hash != NULL)
1051 struct elf_link_hash_entry **cur_sym_hashes;
1053 /* Loop only over the symbols whom been already checked. */
1054 for (cur_sym_hashes = start_hashes; cur_sym_hashes < sym_hashes;
1056 /* If the current symbol is identical to 'sym_hash', that means
1057 the symbol was already adjusted (or at least checked). */
1058 if (*cur_sym_hashes == sym_hash)
1061 /* Don't adjust the symbol again. */
1062 if (cur_sym_hashes < sym_hashes)
1066 if ((sym_hash->root.type == bfd_link_hash_defined
1067 || sym_hash->root.type == bfd_link_hash_defweak)
1068 && sym_hash->root.u.def.section == sec
1069 && sym_hash->root.u.def.value == loc)
1077 ft32_elf_relax_section
1080 struct bfd_link_info * link_info,
1081 bfd_boolean * again)
1083 Elf_Internal_Rela * free_relocs = NULL;
1084 Elf_Internal_Rela * internal_relocs;
1085 Elf_Internal_Rela * irelend;
1086 Elf_Internal_Rela * irel;
1087 bfd_byte * contents = NULL;
1088 Elf_Internal_Shdr * symtab_hdr;
1089 Elf_Internal_Sym * isymbuf = NULL;
1091 /* Assume nothing changes. */
1094 /* We don't have to do anything for a relocatable link, if
1095 this section does not have relocs, or if this is not a
1097 if (bfd_link_relocatable (link_info)
1098 || (sec->flags & SEC_RELOC) == 0
1099 || sec->reloc_count == 0
1100 || (sec->flags & SEC_CODE) == 0)
1103 /* Get the section contents. */
1104 if (elf_section_data (sec)->this_hdr.contents != NULL)
1105 contents = elf_section_data (sec)->this_hdr.contents;
1106 /* Go get them off disk. */
1109 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
1111 elf_section_data (sec)->this_hdr.contents = contents;
1114 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1116 /* Read this BFD's local symbols if we haven't done so already. */
1117 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1119 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1120 if (isymbuf == NULL)
1121 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1122 symtab_hdr->sh_info, 0,
1124 if (isymbuf == NULL)
1126 symtab_hdr->contents = (unsigned char *) isymbuf;
1129 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
1130 link_info->keep_memory);
1131 if (internal_relocs == NULL)
1133 if (! link_info->keep_memory)
1134 free_relocs = internal_relocs;
1136 /* Walk through them looking for relaxing opportunities. */
1137 irelend = internal_relocs + sec->reloc_count;
1139 /* Test every adjacent pair of relocs. If both have shortcodes,
1140 fuse them and delete the relocs. */
1141 irel = internal_relocs;
1142 while (irel < irelend - 1)
1144 Elf_Internal_Rela * irel_next = irel + 1;
1145 unsigned int sc0, sc1;
1148 pc = irel->r_offset;
1150 if (((pc + 4) == (irel_next->r_offset))
1151 && ft32_reloc_shortable (abfd, sec, isymbuf, contents, pc, irel,
1153 && ft32_reloc_shortable (abfd, sec, isymbuf, contents, pc,
1155 && !elf32_ft32_relax_is_branch_target (link_info, abfd, sec,
1156 irel_next->r_offset))
1158 unsigned int code30 = (sc1 << 15) | sc0;
1159 unsigned int code27 = code30 >> 3;
1160 unsigned int code3 = code30 & 7;
1161 static const unsigned char pat3[] = {2, 3, 4, 5, 6, 9, 10, 14};
1162 unsigned int pattern = pat3[code3];
1163 unsigned int fused = (pattern << 27) | code27;
1165 /* Move second reloc to same place as first. */
1166 irel_next->r_offset = irel->r_offset;
1168 /* Change both relocs to R_FT32_NONE. */
1170 if (ELF32_R_TYPE (irel->r_info) == R_FT32_18)
1172 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1177 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1181 if (ELF32_R_TYPE (irel_next->r_info) == R_FT32_18)
1183 irel_next->r_info = ELF32_R_INFO (ELF32_R_SYM (irel_next->r_info),
1188 irel_next->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1192 /* Replace the first insn with the fused version. */
1193 bfd_put_32 (abfd, fused, contents + irel->r_offset);
1195 /* Delete the second insn. */
1196 if (!elf32_ft32_relax_delete_bytes (link_info, abfd, sec,
1197 irel->r_offset + 4, 4))
1200 /* That will change things, so, we should relax again.
1201 Note that this is not required, and it may be slow. */
1213 && symtab_hdr->contents != (unsigned char *) isymbuf)
1215 if (! link_info->keep_memory)
1218 /* Cache the symbols for elf_link_input_bfd. */
1219 symtab_hdr->contents = (unsigned char *) isymbuf;
1222 if (contents != NULL
1223 && elf_section_data (sec)->this_hdr.contents != contents)
1225 if (! link_info->keep_memory)
1228 /* Cache the section contents for elf_link_input_bfd. */
1229 elf_section_data (sec)->this_hdr.contents = contents;
1233 if (internal_relocs != NULL
1234 && elf_section_data (sec)->relocs != internal_relocs)
1235 free (internal_relocs);
1240 if (free_relocs != NULL)
1246 #define ELF_ARCH bfd_arch_ft32
1247 #define ELF_MACHINE_CODE EM_FT32
1248 #define ELF_MAXPAGESIZE 0x1
1250 #define TARGET_LITTLE_SYM ft32_elf32_vec
1251 #define TARGET_LITTLE_NAME "elf32-ft32"
1253 #define elf_info_to_howto_rel NULL
1254 #define elf_info_to_howto ft32_info_to_howto_rela
1255 #define elf_backend_relocate_section ft32_elf_relocate_section
1257 #define elf_backend_can_gc_sections 1
1258 #define elf_backend_rela_normal 1
1260 #define bfd_elf32_bfd_reloc_type_lookup ft32_reloc_type_lookup
1261 #define bfd_elf32_bfd_reloc_name_lookup ft32_reloc_name_lookup
1263 #define bfd_elf32_bfd_relax_section ft32_elf_relax_section
1265 #include "elf32-target.h"