disasm.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Disassemble support for GDB.
  2. Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
  3. Free Software Foundation, Inc.
  4. Copyright 2007 Kevin Kofler
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. Boston, MA 02110-1301, USA. */
  18. #include <stdint.h>
  19. #include <limits.h>
  20. #include <stdarg.h>
  21. #include "sysdep.h"
  22. #include "disasm.h"
  23. #include "dis-asm.h"
  24. #include "romcalls.h"
  25. #include "main.h"
  26. /* Disassemble functions.
  27. FIXME: We should get rid of all the duplicate code in gdb that does
  28. the same thing: disassemble_command() and the gdbtk variation. */
  29. /* (TiEmu 20050429 Kevin Kofler) */
  30. static int
  31. sprintf_disasm (void *stream, const char *format, ...)
  32. {
  33. int result;
  34. va_list args;
  35. va_start (args, format);
  36. result = vsprintf ((char *)stream + strlen((char *)stream), format, args);
  37. va_end (args);
  38. return result;
  39. }
  40. static void
  41. strcat_disasm (const char *format, void *stream)
  42. {
  43. strcat ((char *)stream, format);
  44. }
  45. static void
  46. print_address_1 (int32_t reladdr, uint32_t absaddr, int flags, void *stream,
  47. void (*fprintf_f) (void *, const char *, ...),
  48. void (*fputs_f) (const char *, void *))
  49. {
  50. int rcid;
  51. const char *rcname;
  52. PrintAddress (reladdr, absaddr, flags, stream, fprintf_f, fputs_f);
  53. if (((rcid = romcalls_is_addr (absaddr)) != -1)
  54. && ((rcname = romcalls_get_name (rcid)) != NULL))
  55. fprintf_f (stream, " /* tios::%s */", rcname);
  56. }
  57. static void
  58. dis_asm_sprint_address (bfd_vma addr, struct disassemble_info *info)
  59. {
  60. print_address_1 (addr, info->target, info->flags, info->stream,
  61. (void (*) (void *, const char *, ...)) sprintf_disasm, strcat_disasm);
  62. }
  63. struct disassemble_info
  64. gdb_disassemble_info (unsigned char *mem_buf, char *output_buf)
  65. {
  66. struct disassemble_info di;
  67. init_disassemble_info (&di, output_buf, sprintf_disasm);
  68. di.buffer = mem_buf;
  69. di.buffer_length = UINT_MAX;
  70. di.print_address_func = dis_asm_sprint_address;
  71. di.flavour = bfd_target_unknown_flavour;
  72. di.arch = bfd_arch_m68k;
  73. di.mach = bfd_mach_m68000;
  74. di.endian = BFD_ENDIAN_BIG;
  75. disassemble_init_for_target (&di);
  76. return di;
  77. }
  78. uint32_t Dasm68000(unsigned char *mem_buf, char *output_buf, uint32_t addr)
  79. {
  80. struct disassemble_info di = gdb_disassemble_info(mem_buf, output_buf);
  81. uint32_t offset;
  82. *output_buf = 0;
  83. di.buffer_vma = addr;
  84. offset = print_insn_m68k(addr, &di);
  85. return offset;
  86. }