dis-buf.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* Disassemble from a buffer, for GNU.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. This file is part of the GNU opcodes library.
  4. This library 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 3, or (at your option)
  7. any later version.
  8. It is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  11. 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 > max_addr_offset
  34. || memaddr - info->buffer_vma + end_addr_offset > max_addr_offset
  35. || (info->stop_vma && (memaddr >= info->stop_vma
  36. || memaddr + end_addr_offset > info->stop_vma)))
  37. /* Out of bounds. Use EIO because GDB uses it. */
  38. return EIO;
  39. memcpy (myaddr, info->buffer + octets, length);
  40. return 0;
  41. }
  42. /* Print an error message. We can assume that this is in response to
  43. an error return from buffer_read_memory. */
  44. void
  45. perror_memory (int status,
  46. bfd_vma memaddr,
  47. struct disassemble_info *info)
  48. {
  49. if (status != EIO)
  50. /* Can't happen. */
  51. info->fprintf_func (info->stream, _("Unknown error %d\n"), status);
  52. else
  53. {
  54. char buf[30];
  55. /* Actually, address between memaddr and memaddr + len was
  56. out of bounds. */
  57. sprintf_vma (buf, memaddr);
  58. info->fprintf_func (info->stream,
  59. _("Address 0x%s is out of bounds.\n"), buf);
  60. }
  61. }
  62. /* This could be in a separate file, to save miniscule amounts of space
  63. in statically linked executables. */
  64. /* Just print the address is hex. This is included for completeness even
  65. though both GDB and objdump provide their own (to print symbolic
  66. addresses). */
  67. void
  68. generic_print_address (bfd_vma addr, struct disassemble_info *info)
  69. {
  70. char buf[30];
  71. sprintf_vma (buf, addr);
  72. (*info->fprintf_func) (info->stream, "0x%s", buf);
  73. }
  74. /* Just return true. */
  75. int
  76. generic_symbol_at_address (bfd_vma addr ATTRIBUTE_UNUSED,
  77. struct disassemble_info *info ATTRIBUTE_UNUSED)
  78. {
  79. return 1;
  80. }
  81. /* Just return TRUE. */
  82. bfd_boolean
  83. generic_symbol_is_valid (asymbol * sym ATTRIBUTE_UNUSED,
  84. struct disassemble_info *info ATTRIBUTE_UNUSED)
  85. {
  86. return TRUE;
  87. }