+/* Far trampoline generation. */
+
+/* Build a 68HC11 trampoline stub. */
+static bfd_boolean
+m68hc11_elf_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
+{
+ struct elf32_m68hc11_stub_hash_entry *stub_entry;
+ struct bfd_link_info *info;
+ struct m68hc11_elf_link_hash_table *htab;
+ asection *stub_sec;
+ bfd *stub_bfd;
+ bfd_byte *loc;
+ bfd_vma sym_value, phys_page, phys_addr;
+
+ /* Massage our args to the form they really have. */
+ stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
+ info = (struct bfd_link_info *) in_arg;
+
+ htab = m68hc11_elf_hash_table (info);
+ if (htab == NULL)
+ return FALSE;
+
+ stub_sec = stub_entry->stub_sec;
+
+ /* Make a note of the offset within the stubs for this entry. */
+ stub_entry->stub_offset = stub_sec->size;
+ stub_sec->size += 10;
+ loc = stub_sec->contents + stub_entry->stub_offset;
+
+ stub_bfd = stub_sec->owner;
+
+ /* Create the trampoline call stub:
+
+ pshb
+ ldab #%page(symbol)
+ ldy #%addr(symbol)
+ jmp __trampoline
+
+ */
+ sym_value = (stub_entry->target_value
+ + stub_entry->target_section->output_offset
+ + stub_entry->target_section->output_section->vma);
+ phys_addr = m68hc11_phys_addr (&htab->pinfo, sym_value);
+ phys_page = m68hc11_phys_page (&htab->pinfo, sym_value);
+
+ /* pshb; ldab #%page(sym) */
+ bfd_put_8 (stub_bfd, 0x37, loc);
+ bfd_put_8 (stub_bfd, 0xC6, loc + 1);
+ bfd_put_8 (stub_bfd, phys_page, loc + 2);
+ loc += 3;
+
+ /* ldy #%addr(sym) */
+ bfd_put_8 (stub_bfd, 0x18, loc);
+ bfd_put_8 (stub_bfd, 0xCE, loc + 1);
+ bfd_put_16 (stub_bfd, phys_addr, loc + 2);
+ loc += 4;
+
+ /* jmp __trampoline */
+ bfd_put_8 (stub_bfd, 0x7E, loc);
+ bfd_put_16 (stub_bfd, htab->pinfo.trampoline_addr, loc + 1);
+
+ return TRUE;
+}
+
+/* As above, but don't actually build the stub. Just bump offset so
+ we know stub section sizes. */
+
+static bfd_boolean
+m68hc11_elf_size_one_stub (struct bfd_hash_entry *gen_entry,
+ void *in_arg ATTRIBUTE_UNUSED)
+{
+ struct elf32_m68hc11_stub_hash_entry *stub_entry;
+
+ /* Massage our args to the form they really have. */
+ stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
+
+ stub_entry->stub_sec->size += 10;
+ return TRUE;
+}
+
+/* Create a 68HC11 ELF linker hash table. */
+
+static struct bfd_link_hash_table *
+m68hc11_elf_bfd_link_hash_table_create (bfd *abfd)
+{
+ struct m68hc11_elf_link_hash_table *ret;
+
+ ret = m68hc11_elf_hash_table_create (abfd);
+ if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
+ return NULL;
+
+ ret->size_one_stub = m68hc11_elf_size_one_stub;
+ ret->build_one_stub = m68hc11_elf_build_one_stub;
+
+ return &ret->root.root;
+}
+
+\f
+/* 68HC11 Linker Relaxation. */
+
+struct m68hc11_direct_relax
+{
+ const char *name;
+ unsigned char code;
+ unsigned char direct_code;
+} m68hc11_direct_relax_table[] = {
+ { "adca", 0xB9, 0x99 },
+ { "adcb", 0xF9, 0xD9 },
+ { "adda", 0xBB, 0x9B },
+ { "addb", 0xFB, 0xDB },
+ { "addd", 0xF3, 0xD3 },
+ { "anda", 0xB4, 0x94 },
+ { "andb", 0xF4, 0xD4 },
+ { "cmpa", 0xB1, 0x91 },
+ { "cmpb", 0xF1, 0xD1 },
+ { "cpd", 0xB3, 0x93 },
+ { "cpxy", 0xBC, 0x9C },
+/* { "cpy", 0xBC, 0x9C }, */
+ { "eora", 0xB8, 0x98 },
+ { "eorb", 0xF8, 0xD8 },
+ { "jsr", 0xBD, 0x9D },
+ { "ldaa", 0xB6, 0x96 },
+ { "ldab", 0xF6, 0xD6 },
+ { "ldd", 0xFC, 0xDC },
+ { "lds", 0xBE, 0x9E },
+ { "ldxy", 0xFE, 0xDE },
+ /* { "ldy", 0xFE, 0xDE },*/
+ { "oraa", 0xBA, 0x9A },
+ { "orab", 0xFA, 0xDA },
+ { "sbca", 0xB2, 0x92 },
+ { "sbcb", 0xF2, 0xD2 },
+ { "staa", 0xB7, 0x97 },
+ { "stab", 0xF7, 0xD7 },
+ { "std", 0xFD, 0xDD },
+ { "sts", 0xBF, 0x9F },
+ { "stxy", 0xFF, 0xDF },
+ /* { "sty", 0xFF, 0xDF },*/
+ { "suba", 0xB0, 0x90 },
+ { "subb", 0xF0, 0xD0 },
+ { "subd", 0xB3, 0x93 },
+ { 0, 0, 0 }
+};
+
+static struct m68hc11_direct_relax *
+find_relaxable_insn (unsigned char code)
+{
+ int i;
+
+ for (i = 0; m68hc11_direct_relax_table[i].name; i++)
+ if (m68hc11_direct_relax_table[i].code == code)
+ return &m68hc11_direct_relax_table[i];