dis-buf.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* Disassemble from a buffer, for GNU.
  2. Copyright 1993, 1994, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2005
  3. Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "dis-asm.h"
  18. #include <errno.h>
  19. #include "opintl.h"
  20. /* Get LENGTH bytes from info's buffer, at target address memaddr.
  21. Transfer them to myaddr. */
  22. int
  23. buffer_read_memory (bfd_vma memaddr,
  24. bfd_byte *myaddr,
  25. unsigned int length,
  26. struct disassemble_info *info)
  27. {
  28. unsigned int opb = info->octets_per_byte;
  29. unsigned int end_addr_offset = length / opb;
  30. unsigned int max_addr_offset = info->buffer_length / opb;
  31. unsigned int octets = (memaddr - info->buffer_vma) * opb;
  32. if (memaddr < info->buffer_vma
  33. || memaddr - info->buffer_vma + end_addr_offset > max_addr_offset)
  34. /* Out of bounds. Use EIO because GDB uses it. */
  35. return EIO;
  36. memcpy (myaddr, info->buffer + octets, length);
  37. return 0;
  38. }
  39. /* Print an error message. We can assume that this is in response to
  40. an error return from buffer_read_memory. */
  41. void
  42. perror_memory (int status,
  43. bfd_vma memaddr,
  44. struct disassemble_info *info)
  45. {
  46. if (status != EIO)
  47. /* Can't happen. */
  48. info->fprintf_func (info->stream, _("Unknown error %d\n"), status);
  49. else
  50. {
  51. char buf[30];
  52. /* Actually, address between memaddr and memaddr + len was
  53. out of bounds. */
  54. sprintf_vma (buf, memaddr);
  55. info->fprintf_func (info->stream,
  56. _("Address 0x%s is out of bounds.\n"), buf);
  57. }
  58. }
  59. /* This could be in a separate file, to save miniscule amounts of space
  60. in statically linked executables. */
  61. /* Just print the address is hex. This is included for completeness even
  62. though both GDB and objdump provide their own (to print symbolic
  63. addresses). */
  64. void
  65. generic_print_address (bfd_vma addr, struct disassemble_info *info)
  66. {
  67. char buf[30];
  68. sprintf_vma (buf, addr);
  69. (*info->fprintf_func) (info->stream, "0x%s", buf);
  70. }
  71. /* Just return true. */
  72. int
  73. generic_symbol_at_address (bfd_vma addr ATTRIBUTE_UNUSED,
  74. struct disassemble_info *info ATTRIBUTE_UNUSED)
  75. {
  76. return 1;
  77. }
  78. /* Just return TRUE. */
  79. bfd_boolean
  80. generic_symbol_is_valid (asymbol * sym ATTRIBUTE_UNUSED,
  81. struct disassemble_info *info ATTRIBUTE_UNUSED)
  82. {
  83. return TRUE;
  84. }