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