+ if (isym->st_shndx == shndx
+ && addr <= isym->st_value
+ && isym->st_value < endaddr)
+ 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)
+ {
+ if (addr <= sym_hash->root.u.def.value
+ && sym_hash->root.u.def.value < endaddr)
+ sym_hash->root.u.def.value += count;
+ }
+ }
+
+ return;
+}
+
+/* Delete some bytes from a section while relaxing. */
+
+static bfd_boolean
+ip2k_elf_relax_delete_bytes (bfd *abfd,
+ asection *sec,
+ bfd_vma addr,
+ int count)
+{
+ bfd_byte *contents = elf_section_data (sec)->this_hdr.contents;
+ bfd_vma endaddr = sec->size;
+
+ /* Actually delete the bytes. */
+ memmove (contents + addr, contents + addr + count,
+ endaddr - addr - count);
+
+ sec->size -= count;
+
+ adjust_all_relocations (abfd, sec, addr + count, endaddr, -count, 0);
+ return TRUE;
+}
+
+static bfd_boolean
+ip2k_delete_page_insn (bfd *abfd ATTRIBUTE_UNUSED,
+ asection *sec,
+ Elf_Internal_Rela *irel,
+ bfd_boolean *again,
+ struct misc *misc)
+{
+ /* Note that we've changed the relocs, section contents, etc. */
+ elf_section_data (sec)->relocs = misc->irelbase;
+ elf_section_data (sec)->this_hdr.contents = misc->contents;
+ misc->symtab_hdr->contents = (bfd_byte *) misc->isymbuf;
+
+ /* Fix the relocation's type. */
+ irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_IP2K_NONE);
+
+ /* Delete the PAGE insn. */
+ if (!ip2k_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 2))
+ return FALSE;
+
+ /* Modified => will need to iterate relaxation again. */
+ *again = TRUE;
+
+ return TRUE;
+}
+
+static bfd_boolean
+ip2k_relax_switch_table_128 (bfd *abfd ATTRIBUTE_UNUSED,
+ asection *sec,
+ Elf_Internal_Rela *irel,
+ bfd_boolean *again,
+ struct misc *misc)
+{
+ Elf_Internal_Rela *irelend = misc->irelbase + sec->reloc_count;
+ Elf_Internal_Rela *ireltest = irel;
+ bfd_byte code[4];
+ bfd_vma addr;
+
+ /* Test all page instructions. */
+ addr = irel->r_offset;
+ while (1)
+ {
+ if (addr + 4 > sec->size)
+ break;
+
+ ip2k_get_mem (abfd, misc->contents + addr, 4, code);
+ if ((! IS_PAGE_OPCODE (code + 0))
+ || (! IS_JMP_OPCODE (code + 2)))
+ break;
+
+ /* Validate relocation entry (every entry should have a matching
+ relocation entry). */
+ if (ireltest >= irelend)
+ {
+ _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
+ return FALSE;
+ }
+
+ if (ireltest->r_offset != addr)
+ {
+ _bfd_error_handler (_("ip2k relaxer: switch table without complete matching relocation information."));
+ return FALSE;
+ }
+
+ if (! ip2k_test_page_insn (abfd, sec, ireltest, misc))
+ /* Un-removable page insn => nothing can be done. */
+ return TRUE;
+
+ addr += 4;
+ ireltest += 2;
+ }
+
+ /* Relaxable. Adjust table header. */
+ ip2k_get_mem (abfd, misc->contents + irel->r_offset - 4, 4, code);
+ if ((! IS_ADD_W_WREG_OPCODE (code + 0))
+ || (! IS_ADD_PCL_W_OPCODE (code + 2)))
+ {
+ _bfd_error_handler (_("ip2k relaxer: switch table header corrupt."));
+ return FALSE;
+ }
+
+ if (!ip2k_elf_relax_delete_bytes (abfd, sec, irel->r_offset - 4, 2))
+ return FALSE;
+
+ *again = TRUE;
+
+ /* Delete all page instructions in table. */
+ while (irel < ireltest)
+ {
+ if (!ip2k_delete_page_insn (abfd, sec, irel, again, misc))
+ return FALSE;
+ irel += 2;