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