gl: clear w, h on reinit
[libpicofe.git] / linux / host_dasm.c
index cd2db69..a5e66c9 100644 (file)
@@ -1,3 +1,14 @@
+/*
+ * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
+ *
+ * This work is licensed under the terms of any of these licenses
+ * (at your option):
+ *  - GNU GPL, version 2 or later.
+ *  - GNU LGPL, version 2.1 or later.
+ *  - MAME license.
+ * See the COPYING file in the top-level directory.
+ */
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 extern char **g_argv;
 
 static struct disassemble_info di;
+static disassembler_ftype print_insn_func;
 
-#ifdef ARM
+#if defined __arm__
 #define print_insn_func print_insn_little_arm
 #define BFD_ARCH bfd_arch_arm
-#define BFD_MACH bfd_mach_arm_4T
-#else
+#define BFD_MACH bfd_mach_arm_unknown
+#define DASM_OPTS "reg-names-std"
+#elif defined __aarch64__
+#define print_insn_func print_insn_aarch64
+#define BFD_ARCH bfd_arch_aarch64
+#define BFD_MACH bfd_mach_aarch64
+#define DASM_OPTS NULL
+#elif defined __mips__
+#define print_insn_func print_insn_little_mips
+#define BFD_ARCH bfd_arch_mips
+#define BFD_MACH bfd_mach_mipsisa64r2
+#define DASM_OPTS NULL
+#elif defined __riscv
+//#define print_insn_func print_insn_riscv
+#define BFD_ARCH bfd_arch_riscv
+#define BFD_MACH bfd_mach_riscv64
+#define DASM_OPTS NULL
+#elif defined __powerpc__
+#define print_insn_func print_insn_little_powerpc
+#define BFD_ARCH bfd_arch_powerpc
+#define BFD_MACH bfd_mach_ppc64
+#define DASM_OPTS NULL
+#elif defined(__x86_64__) || defined(__i386__)
 #define print_insn_func print_insn_i386_intel
 #define BFD_ARCH bfd_arch_i386
+#ifdef __x86_64__
+#define BFD_MACH bfd_mach_x86_64_intel_syntax
+#else
 #define BFD_MACH bfd_mach_i386_i386_intel_syntax
 #endif
+#define DASM_OPTS NULL
+#else
+#error "missing arch support"
+#endif
 
 /* symbols */
 static asymbol **symbols;
@@ -121,7 +161,7 @@ static int
 dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len,
                      struct disassemble_info *info)
 {
-  memcpy(myaddr, (void *)(int)memaddr, len);
+  memcpy(myaddr, (void *)memaddr, len);
   return 0;
 }
 
@@ -129,7 +169,7 @@ static void
 dis_asm_memory_error(int status, bfd_vma memaddr,
                       struct disassemble_info *info)
 {
-  fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr);
+  fprintf(stderr, "memory_error %p\n", (void *)(long)memaddr);
 }
 
 static void
@@ -156,10 +196,20 @@ static int insn_printf(void *f, const char *format, ...)
   return n;
 }
 
+static int print_insn_hex(bfd_vma addr, struct disassemble_info *info)
+{
+  unsigned op;
+
+  dis_asm_read_memory(addr, (bfd_byte *)&op, 4, info);
+  printf("%p %08lx",(void *)addr, (long)op);
+  return 4;
+}
+
 static void host_dasm_init(void)
 {
   bfd_init();
-  slurp_symtab(g_argv[0]);
+  if (g_argv && g_argv[0])
+    slurp_symtab(g_argv[0]);
 
   init_disassemble_info(&di, NULL, insn_printf);
   di.flavour = bfd_target_unknown_flavour;
@@ -170,13 +220,18 @@ static void host_dasm_init(void)
   di.arch = BFD_ARCH;
   di.mach = BFD_MACH;
   di.endian = BFD_ENDIAN_LITTLE;
+  di.disassembler_options = DASM_OPTS;
   disassemble_init_for_target(&di);
+#ifndef print_insn_func
+  print_insn_func = disassembler(BFD_ARCH, 0, BFD_MACH, NULL);
+  if (!print_insn_func) print_insn_func = print_insn_hex;
+#endif
   init_done = 1;
 }
 
 void host_dasm(void *addr, int len)
 {
-  bfd_vma vma_end, vma = (bfd_vma)(long)addr;
+  bfd_vma vma_end, vma = (bfd_vma)addr;
   const char *name;
 
   if (!init_done)
@@ -188,7 +243,7 @@ void host_dasm(void *addr, int len)
     if (name != NULL)
       printf("%s:\n", name);
 
-    printf("      %p ", (void *)(long)vma);
+    printf("   %08lx ", (long)vma);
     vma += print_insn_func(vma, &di);
     printf("\n");
   }