+/* These functions handle relaxing for the msp430.
+ Relaxation required only in two cases:
+ - Bad hand coding like jumps from one section to another or
+ from file to file.
+ - Sibling calls. This will affect onlu 'jump label' polymorph. Without
+ relaxing this enlarges code by 2 bytes. Sibcalls implemented but
+ do not work in gcc's port by the reason I do not know.
+ Anyway, if a relaxation required, user should pass -relax option to the
+ linker.
+
+ There are quite a few relaxing opportunities available on the msp430:
+
+ ================================================================
+
+ 1. 3 words -> 1 word
+
+ eq == jeq label jne +4; br lab
+ ne != jne label jeq +4; br lab
+ lt < jl label jge +4; br lab
+ ltu < jlo label lhs +4; br lab
+ ge >= jge label jl +4; br lab
+ geu >= jhs label jlo +4; br lab
+
+ 2. 4 words -> 1 word
+
+ ltn < jn jn +2; jmp +4; br lab
+
+ 3. 4 words -> 2 words
+
+ gt > jeq +2; jge label jeq +6; jl +4; br label
+ gtu > jeq +2; jhs label jeq +6; jlo +4; br label
+
+ 4. 4 words -> 2 words and 2 labels
+
+ leu <= jeq label; jlo label jeq +2; jhs +4; br label
+ le <= jeq label; jl label jeq +2; jge +4; br label
+ =================================================================
+
+ codemap for first cases is (labels masked ):
+ eq: 0x2002,0x4010,0x0000 -> 0x2400
+ ne: 0x2402,0x4010,0x0000 -> 0x2000
+ lt: 0x3402,0x4010,0x0000 -> 0x3800
+ ltu: 0x2c02,0x4010,0x0000 -> 0x2800
+ ge: 0x3802,0x4010,0x0000 -> 0x3400
+ geu: 0x2802,0x4010,0x0000 -> 0x2c00
+
+ second case:
+ ltn: 0x3001,0x3c02,0x4010,0x0000 -> 0x3000
+
+ third case:
+ gt: 0x2403,0x3802,0x4010,0x0000 -> 0x2401,0x3400
+ gtu: 0x2403,0x2802,0x4010,0x0000 -> 0x2401,0x2c00
+
+ fourth case:
+ leu: 0x2401,0x2c02,0x4010,0x0000 -> 0x2400,0x2800
+ le: 0x2401,0x3402,0x4010,0x0000 -> 0x2400,0x3800
+
+ Unspecified case :)
+ jump: 0x4010,0x0000 -> 0x3c00. */
+
+#define NUMB_RELAX_CODES 12
+static struct rcodes_s
+{
+ int f0, f1; /* From code. */
+ int t0, t1; /* To code. */
+ int labels; /* Position of labels: 1 - one label at first
+ word, 2 - one at second word, 3 - two
+ labels at both. */
+ int cdx; /* Words to match. */
+ int bs; /* Shrink bytes. */
+ int off; /* Offset from old label for new code. */
+ int ncl; /* New code length. */
+} rcode[] =
+{/* lab,cdx,bs,off,ncl */
+ { 0x0000, 0x0000, 0x3c00, 0x0000, 1, 0, 2, 2, 2}, /* jump */
+ { 0x0000, 0x2002, 0x2400, 0x0000, 1, 1, 4, 4, 2}, /* eq */
+ { 0x0000, 0x2402, 0x2000, 0x0000, 1, 1, 4, 4, 2}, /* ne */
+ { 0x0000, 0x3402, 0x3800, 0x0000, 1, 1, 4, 4, 2}, /* lt */
+ { 0x0000, 0x2c02, 0x2800, 0x0000, 1, 1, 4, 4, 2}, /* ltu */
+ { 0x0000, 0x3802, 0x3400, 0x0000, 1, 1, 4, 4, 2}, /* ge */
+ { 0x0000, 0x2802, 0x2c00, 0x0000, 1, 1, 4, 4, 2}, /* geu */
+ { 0x3001, 0x3c02, 0x3000, 0x0000, 1, 2, 6, 6, 2}, /* ltn */
+ { 0x2403, 0x3802, 0x2401, 0x3400, 2, 2, 4, 6, 4}, /* gt */
+ { 0x2403, 0x2802, 0x2401, 0x2c00, 2, 2, 4, 6, 4}, /* gtu */
+ { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6, 4}, /* leu , 2 labels */
+ { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6, 4}, /* le , 2 labels */
+ { 0, 0, 0, 0, 0, 0, 0, 0, 0}
+};
+
+/* Return TRUE if a symbol exists at the given address. */
+
+static bfd_boolean
+msp430_elf_symbol_address_p (bfd * abfd,
+ asection * sec,
+ Elf_Internal_Sym * isym,
+ bfd_vma addr)
+{
+ Elf_Internal_Shdr *symtab_hdr;
+ unsigned int sec_shndx;
+ Elf_Internal_Sym *isymend;
+ struct elf_link_hash_entry **sym_hashes;
+ struct elf_link_hash_entry **end_hashes;
+ unsigned int symcount;
+
+ sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
+
+ /* Examine all the local symbols. */
+ symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+ for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
+ if (isym->st_shndx == sec_shndx && isym->st_value == addr)
+ return TRUE;
+
+ symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
+ - symtab_hdr->sh_info);
+ sym_hashes = elf_sym_hashes (abfd);
+ end_hashes = sym_hashes + symcount;
+ for (; sym_hashes < end_hashes; sym_hashes++)
+ {
+ struct elf_link_hash_entry *sym_hash = *sym_hashes;
+
+ if ((sym_hash->root.type == bfd_link_hash_defined
+ || sym_hash->root.type == bfd_link_hash_defweak)
+ && sym_hash->root.u.def.section == sec
+ && sym_hash->root.u.def.value == addr)
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+/* Adjust all local symbols defined as '.section + 0xXXXX' (.section has sec_shndx)
+ referenced from current and other sections */
+static bfd_boolean
+msp430_elf_relax_adjust_locals(bfd * abfd, asection * sec, bfd_vma addr,
+ int count, unsigned int sec_shndx, bfd_vma toaddr)
+{
+ Elf_Internal_Shdr *symtab_hdr;
+ Elf_Internal_Rela *irel;
+ Elf_Internal_Rela *irelend;
+ Elf_Internal_Sym *isym;
+
+ irel = elf_section_data (sec)->relocs;
+ irelend = irel + sec->reloc_count;
+ symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
+ isym = (Elf_Internal_Sym *) symtab_hdr->contents;
+
+ for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
+ {
+ int sidx = ELF32_R_SYM(irel->r_info);
+ Elf_Internal_Sym *lsym = isym + sidx;
+
+ /* Adjust symbols referenced by .sec+0xXX */
+ if (irel->r_addend > addr && irel->r_addend < toaddr
+ && lsym->st_shndx == sec_shndx)
+ irel->r_addend -= count;
+ }
+
+ return TRUE;
+}
+
+/* Delete some bytes from a section while relaxing. */
+
+static bfd_boolean
+msp430_elf_relax_delete_bytes (bfd * abfd, asection * sec, bfd_vma addr,
+ int count)
+{
+ Elf_Internal_Shdr *symtab_hdr;
+ unsigned int sec_shndx;
+ bfd_byte *contents;
+ Elf_Internal_Rela *irel;
+ Elf_Internal_Rela *irelend;
+ bfd_vma toaddr;
+ Elf_Internal_Sym *isym;
+ Elf_Internal_Sym *isymend;
+ struct elf_link_hash_entry **sym_hashes;
+ struct elf_link_hash_entry **end_hashes;
+ unsigned int symcount;
+ asection *p;
+
+ sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
+
+ contents = elf_section_data (sec)->this_hdr.contents;
+
+ toaddr = sec->size;
+
+ irel = elf_section_data (sec)->relocs;
+ irelend = irel + sec->reloc_count;
+
+ /* Actually delete the bytes. */
+ memmove (contents + addr, contents + addr + count,
+ (size_t) (toaddr - addr - count));
+ sec->size -= count;
+
+ /* Adjust all the relocs. */
+ symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
+ isym = (Elf_Internal_Sym *) symtab_hdr->contents;
+ for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
+ {
+ /* Get the new reloc address. */
+ if ((irel->r_offset > addr && irel->r_offset < toaddr))
+ irel->r_offset -= count;
+ }
+
+ for (p = abfd->sections; p != NULL; p = p->next)
+ msp430_elf_relax_adjust_locals(abfd,p,addr,count,sec_shndx,toaddr);
+
+ /* Adjust the local symbols defined in this section. */
+ symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
+ isym = (Elf_Internal_Sym *) symtab_hdr->contents;
+ for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
+ if (isym->st_shndx == sec_shndx
+ && isym->st_value > addr && isym->st_value < toaddr)
+ isym->st_value -= count;
+
+ /* Now adjust the global symbols defined in this section. */
+ symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
+ - symtab_hdr->sh_info);
+ sym_hashes = elf_sym_hashes (abfd);
+ end_hashes = sym_hashes + symcount;
+ for (; sym_hashes < end_hashes; sym_hashes++)
+ {
+ struct elf_link_hash_entry *sym_hash = *sym_hashes;
+
+ if ((sym_hash->root.type == bfd_link_hash_defined
+ || sym_hash->root.type == bfd_link_hash_defweak)
+ && sym_hash->root.u.def.section == sec
+ && sym_hash->root.u.def.value > addr
+ && sym_hash->root.u.def.value < toaddr)
+ sym_hash->root.u.def.value -= count;
+ }
+
+ return TRUE;
+}
+
+
+static bfd_boolean
+msp430_elf_relax_section (bfd * abfd, asection * sec,
+ struct bfd_link_info * link_info,
+ bfd_boolean * again)