+/* Delete some bytes from a section while relaxing. */
+
+static bfd_boolean
+elf32_rl78_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
+ Elf_Internal_Rela *alignment_rel, int force_snip)
+{
+ Elf_Internal_Shdr * symtab_hdr;
+ unsigned int sec_shndx;
+ bfd_byte * contents;
+ Elf_Internal_Rela * irel;
+ Elf_Internal_Rela * irelend;
+ Elf_Internal_Sym * isym;
+ Elf_Internal_Sym * isymend;
+ bfd_vma toaddr;
+ unsigned int symcount;
+ struct elf_link_hash_entry ** sym_hashes;
+ struct elf_link_hash_entry ** end_hashes;
+
+ if (!alignment_rel)
+ force_snip = 1;
+
+ sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
+
+ contents = elf_section_data (sec)->this_hdr.contents;
+
+ /* The deletion must stop at the next alignment boundary, if
+ ALIGNMENT_REL is non-NULL. */
+ toaddr = sec->size;
+ if (alignment_rel)
+ toaddr = alignment_rel->r_offset;
+
+ irel = elf_section_data (sec)->relocs;
+ if (irel == NULL)
+ {
+ _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE);
+ 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));
+
+ /* If we don't have an alignment marker to worry about, we can just
+ shrink the section. Otherwise, we have to fill in the newly
+ created gap with NOP insns (0x03). */
+ if (force_snip)
+ sec->size -= count;
+ else
+ memset (contents + toaddr - count, 0x03, count);
+
+ /* Adjust all the relocs. */
+ for (; irel && irel < irelend; irel++)
+ {
+ /* Get the new reloc address. */
+ if (irel->r_offset > addr
+ && (irel->r_offset < toaddr
+ || (force_snip && irel->r_offset == toaddr)))
+ irel->r_offset -= count;
+
+ /* If we see an ALIGN marker at the end of the gap, we move it
+ to the beginning of the gap, since marking these gaps is what
+ they're for. */
+ if (irel->r_offset == toaddr
+ && ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX
+ && irel->r_addend & RL78_RELAXA_ALIGN)
+ irel->r_offset -= count;
+ }
+
+ /* Adjust the local symbols defined in this section. */
+ symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+ isym = (Elf_Internal_Sym *) symtab_hdr->contents;
+ isymend = isym + symtab_hdr->sh_info;
+
+ for (; isym < isymend; isym++)
+ {
+ /* If the symbol is in the range of memory we just moved, we
+ have to adjust its value. */
+ if (isym->st_shndx == sec_shndx
+ && isym->st_value > addr
+ && isym->st_value < toaddr)
+ isym->st_value -= count;
+
+ /* If the symbol *spans* the bytes we just deleted (i.e. it's
+ *end* is in the moved bytes but it's *start* isn't), then we
+ must adjust its size. */
+ if (isym->st_shndx == sec_shndx
+ && isym->st_value < addr
+ && isym->st_value + isym->st_size > addr
+ && isym->st_value + isym->st_size < toaddr)
+ isym->st_size -= 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)
+ {
+ /* As above, adjust the value if needed. */
+ if (sym_hash->root.u.def.value > addr
+ && sym_hash->root.u.def.value < toaddr)
+ sym_hash->root.u.def.value -= count;
+
+ /* As above, adjust the size if needed. */
+ if (sym_hash->root.u.def.value < addr
+ && sym_hash->root.u.def.value + sym_hash->size > addr
+ && sym_hash->root.u.def.value + sym_hash->size < toaddr)
+ sym_hash->size -= count;
+ }
+ }
+
+ return TRUE;
+}
+
+/* Used to sort relocs by address. If relocs have the same address,
+ we maintain their relative order, except that R_RL78_RH_RELAX
+ alignment relocs must be the first reloc for any given address. */
+
+static void
+reloc_bubblesort (Elf_Internal_Rela * r, int count)
+{
+ int i;
+ bfd_boolean again;
+ bfd_boolean swappit;
+
+ /* This is almost a classic bubblesort. It's the slowest sort, but
+ we're taking advantage of the fact that the relocations are
+ mostly in order already (the assembler emits them that way) and
+ we need relocs with the same address to remain in the same
+ relative order. */
+ again = TRUE;
+ while (again)
+ {
+ again = FALSE;
+ for (i = 0; i < count - 1; i ++)
+ {
+ if (r[i].r_offset > r[i + 1].r_offset)
+ swappit = TRUE;
+ else if (r[i].r_offset < r[i + 1].r_offset)
+ swappit = FALSE;
+ else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX
+ && (r[i + 1].r_addend & RL78_RELAXA_ALIGN))
+ swappit = TRUE;
+ else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX
+ && (r[i + 1].r_addend & RL78_RELAXA_ELIGN)
+ && !(ELF32_R_TYPE (r[i].r_info) == R_RL78_RH_RELAX
+ && (r[i].r_addend & RL78_RELAXA_ALIGN)))
+ swappit = TRUE;
+ else
+ swappit = FALSE;
+
+ if (swappit)
+ {
+ Elf_Internal_Rela tmp;
+
+ tmp = r[i];
+ r[i] = r[i + 1];
+ r[i + 1] = tmp;
+ /* If we do move a reloc back, re-scan to see if it
+ needs to be moved even further back. This avoids
+ most of the O(n^2) behavior for our cases. */
+ if (i > 0)
+ i -= 2;
+ again = TRUE;
+ }
+ }
+ }
+}
+
+
+#define OFFSET_FOR_RELOC(rel, lrel, scale) \
+ rl78_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
+ lrel, abfd, sec, link_info, scale)
+
+static bfd_vma
+rl78_offset_for_reloc (bfd * abfd,
+ Elf_Internal_Rela * rel,
+ Elf_Internal_Shdr * symtab_hdr,
+ Elf_External_Sym_Shndx * shndx_buf ATTRIBUTE_UNUSED,
+ Elf_Internal_Sym * intsyms,
+ Elf_Internal_Rela ** lrel,
+ bfd * input_bfd,
+ asection * input_section,
+ struct bfd_link_info * info,
+ int * scale)
+{
+ bfd_vma symval;
+
+ *scale = 1;
+
+ /* REL is the first of 1..N relocations. We compute the symbol
+ value for each relocation, then combine them if needed. LREL
+ gets a pointer to the last relocation used. */
+ while (1)
+ {
+ unsigned long r_type;
+
+ /* Get the value of the symbol referred to by the reloc. */
+ if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
+ {
+ /* A local symbol. */
+ Elf_Internal_Sym *isym;
+ asection *ssec;
+
+ isym = intsyms + ELF32_R_SYM (rel->r_info);
+
+ if (isym->st_shndx == SHN_UNDEF)
+ ssec = bfd_und_section_ptr;
+ else if (isym->st_shndx == SHN_ABS)
+ ssec = bfd_abs_section_ptr;
+ else if (isym->st_shndx == SHN_COMMON)
+ ssec = bfd_com_section_ptr;
+ else
+ ssec = bfd_section_from_elf_index (abfd,
+ isym->st_shndx);
+
+ /* Initial symbol value. */
+ symval = isym->st_value;
+
+ /* GAS may have made this symbol relative to a section, in
+ which case, we have to add the addend to find the
+ symbol. */
+ if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
+ symval += rel->r_addend;
+
+ if (ssec)
+ {
+ if ((ssec->flags & SEC_MERGE)
+ && ssec->sec_info_type == SEC_INFO_TYPE_MERGE)
+ symval = _bfd_merged_section_offset (abfd, & ssec,
+ elf_section_data (ssec)->sec_info,
+ symval);
+ }
+
+ /* Now make the offset relative to where the linker is putting it. */
+ if (ssec)
+ symval +=
+ ssec->output_section->vma + ssec->output_offset;
+
+ symval += rel->r_addend;
+ }
+ else
+ {
+ unsigned long indx;
+ struct elf_link_hash_entry * h;
+
+ /* An external symbol. */
+ indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
+ h = elf_sym_hashes (abfd)[indx];
+ BFD_ASSERT (h != NULL);
+
+ if (h->root.type != bfd_link_hash_defined
+ && h->root.type != bfd_link_hash_defweak)
+ {
+ /* This appears to be a reference to an undefined
+ symbol. Just ignore it--it will be caught by the
+ regular reloc processing. */
+ if (lrel)
+ *lrel = rel;
+ return 0;
+ }
+
+ symval = (h->root.u.def.value
+ + h->root.u.def.section->output_section->vma
+ + h->root.u.def.section->output_offset);
+
+ symval += rel->r_addend;
+ }
+
+ r_type = ELF32_R_TYPE (rel->r_info);
+ switch (r_type)
+ {
+ case R_RL78_SYM:
+ (void) rl78_compute_complex_reloc (r_type, symval, input_section);
+ break;
+
+ case R_RL78_OPromtop:
+ symval = get_romstart (info, input_bfd, input_section, rel->r_offset);
+ (void) rl78_compute_complex_reloc (r_type, symval, input_section);
+ break;
+
+ case R_RL78_OPramtop:
+ symval = get_ramstart (info, input_bfd, input_section, rel->r_offset);
+ (void) rl78_compute_complex_reloc (r_type, symval, input_section);
+ break;
+
+ case R_RL78_OPneg:
+ case R_RL78_OPadd:
+ case R_RL78_OPsub:
+ case R_RL78_OPmul:
+ case R_RL78_OPdiv:
+ case R_RL78_OPshla:
+ case R_RL78_OPshra:
+ case R_RL78_OPsctsize:
+ case R_RL78_OPscttop:
+ case R_RL78_OPand:
+ case R_RL78_OPor:
+ case R_RL78_OPxor:
+ case R_RL78_OPnot:
+ case R_RL78_OPmod:
+ (void) rl78_compute_complex_reloc (r_type, 0, input_section);
+ break;
+
+ case R_RL78_DIR16UL:
+ case R_RL78_DIR8UL:
+ case R_RL78_ABS16UL:
+ case R_RL78_ABS8UL:
+ *scale = 4;
+ goto reloc_computes_value;
+
+ case R_RL78_DIR16UW:
+ case R_RL78_DIR8UW:
+ case R_RL78_ABS16UW:
+ case R_RL78_ABS8UW:
+ *scale = 2;
+ goto reloc_computes_value;
+
+ default:
+ reloc_computes_value:
+ symval = rl78_compute_complex_reloc (r_type, symval, input_section);
+ /* Fall through. */
+ case R_RL78_DIR32:
+ case R_RL78_DIR24S:
+ case R_RL78_DIR16:
+ case R_RL78_DIR16U:
+ case R_RL78_DIR16S:
+ case R_RL78_DIR24S_PCREL:
+ case R_RL78_DIR16S_PCREL:
+ case R_RL78_DIR8S_PCREL:
+ if (lrel)
+ *lrel = rel;
+ return symval;
+ }
+
+ rel ++;
+ }
+}
+
+struct {
+ int prefix; /* or -1 for "no prefix" */
+ int insn; /* or -1 for "end of list" */
+ int insn_for_saddr; /* or -1 for "no alternative" */
+ int insn_for_sfr; /* or -1 for "no alternative" */
+} relax_addr16[] = {
+ { -1, 0x02, 0x06, -1 }, /* ADDW AX, !addr16 */
+ { -1, 0x22, 0x26, -1 }, /* SUBW AX, !addr16 */
+ { -1, 0x42, 0x46, -1 }, /* CMPW AX, !addr16 */
+ { -1, 0x40, 0x4a, -1 }, /* CMP !addr16, #byte */
+
+ { -1, 0x0f, 0x0b, -1 }, /* ADD A, !addr16 */
+ { -1, 0x1f, 0x1b, -1 }, /* ADDC A, !addr16 */
+ { -1, 0x2f, 0x2b, -1 }, /* SUB A, !addr16 */
+ { -1, 0x3f, 0x3b, -1 }, /* SUBC A, !addr16 */
+ { -1, 0x4f, 0x4b, -1 }, /* CMP A, !addr16 */
+ { -1, 0x5f, 0x5b, -1 }, /* AND A, !addr16 */
+ { -1, 0x6f, 0x6b, -1 }, /* OR A, !addr16 */
+ { -1, 0x7f, 0x7b, -1 }, /* XOR A, !addr16 */
+
+ { -1, 0x8f, 0x8d, 0x8e }, /* MOV A, !addr16 */
+ { -1, 0x9f, 0x9d, 0x9e }, /* MOV !addr16, A */
+ { -1, 0xaf, 0xad, 0xae }, /* MOVW AX, !addr16 */
+ { -1, 0xbf, 0xbd, 0xbe }, /* MOVW !addr16, AX */
+ { -1, 0xcf, 0xcd, 0xce }, /* MOVW !addr16, #word */
+
+ { -1, 0xa0, 0xa4, -1 }, /* INC !addr16 */
+ { -1, 0xa2, 0xa6, -1 }, /* INCW !addr16 */
+ { -1, 0xb0, 0xb4, -1 }, /* DEC !addr16 */
+ { -1, 0xb2, 0xb6, -1 }, /* DECW !addr16 */
+
+ { -1, 0xd5, 0xd4, -1 }, /* CMP0 !addr16 */
+ { -1, 0xe5, 0xe4, -1 }, /* ONEB !addr16 */
+ { -1, 0xf5, 0xf4, -1 }, /* CLRB !addr16 */
+
+ { -1, 0xd9, 0xd8, -1 }, /* MOV X, !addr16 */
+ { -1, 0xe9, 0xe8, -1 }, /* MOV B, !addr16 */
+ { -1, 0xf9, 0xf8, -1 }, /* MOV C, !addr16 */
+ { -1, 0xdb, 0xda, -1 }, /* MOVW BC, !addr16 */
+ { -1, 0xeb, 0xea, -1 }, /* MOVW DE, !addr16 */
+ { -1, 0xfb, 0xfa, -1 }, /* MOVW HL, !addr16 */
+
+ { 0x61, 0xaa, 0xa8, -1 }, /* XCH A, !addr16 */
+
+ { 0x71, 0x00, 0x02, 0x0a }, /* SET1 !addr16.0 */
+ { 0x71, 0x10, 0x12, 0x1a }, /* SET1 !addr16.0 */
+ { 0x71, 0x20, 0x22, 0x2a }, /* SET1 !addr16.0 */
+ { 0x71, 0x30, 0x32, 0x3a }, /* SET1 !addr16.0 */
+ { 0x71, 0x40, 0x42, 0x4a }, /* SET1 !addr16.0 */
+ { 0x71, 0x50, 0x52, 0x5a }, /* SET1 !addr16.0 */
+ { 0x71, 0x60, 0x62, 0x6a }, /* SET1 !addr16.0 */
+ { 0x71, 0x70, 0x72, 0x7a }, /* SET1 !addr16.0 */
+
+ { 0x71, 0x08, 0x03, 0x0b }, /* CLR1 !addr16.0 */
+ { 0x71, 0x18, 0x13, 0x1b }, /* CLR1 !addr16.0 */
+ { 0x71, 0x28, 0x23, 0x2b }, /* CLR1 !addr16.0 */
+ { 0x71, 0x38, 0x33, 0x3b }, /* CLR1 !addr16.0 */
+ { 0x71, 0x48, 0x43, 0x4b }, /* CLR1 !addr16.0 */
+ { 0x71, 0x58, 0x53, 0x5b }, /* CLR1 !addr16.0 */
+ { 0x71, 0x68, 0x63, 0x6b }, /* CLR1 !addr16.0 */
+ { 0x71, 0x78, 0x73, 0x7b }, /* CLR1 !addr16.0 */
+
+ { -1, -1, -1, -1 }
+};
+
+/* Relax one section. */
+