dis-buf.c revision 1.3 1 1.1 christos /* Disassemble from a buffer, for GNU.
2 1.3 christos Copyright (C) 1993-2015 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.1 christos unsigned int end_addr_offset = length / opb;
36 1.3 christos unsigned int max_addr_offset = info->buffer_length / opb;
37 1.1 christos unsigned int 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 char buf[30];
65 1.1 christos
66 1.1 christos /* Actually, address between memaddr and memaddr + len was
67 1.1 christos out of bounds. */
68 1.1 christos sprintf_vma (buf, memaddr);
69 1.1 christos info->fprintf_func (info->stream,
70 1.1 christos _("Address 0x%s is out of bounds.\n"), buf);
71 1.1 christos }
72 1.1 christos }
73 1.1 christos
74 1.1 christos /* This could be in a separate file, to save miniscule amounts of space
75 1.1 christos in statically linked executables. */
76 1.1 christos
77 1.1 christos /* Just print the address is hex. This is included for completeness even
78 1.1 christos though both GDB and objdump provide their own (to print symbolic
79 1.1 christos addresses). */
80 1.1 christos
81 1.1 christos void
82 1.1 christos generic_print_address (bfd_vma addr, struct disassemble_info *info)
83 1.1 christos {
84 1.1 christos char buf[30];
85 1.1 christos
86 1.1 christos sprintf_vma (buf, addr);
87 1.1 christos (*info->fprintf_func) (info->stream, "0x%s", buf);
88 1.1 christos }
89 1.1 christos
90 1.1 christos /* Just return true. */
91 1.1 christos
92 1.1 christos int
93 1.1 christos generic_symbol_at_address (bfd_vma addr ATTRIBUTE_UNUSED,
94 1.1 christos struct disassemble_info *info ATTRIBUTE_UNUSED)
95 1.1 christos {
96 1.1 christos return 1;
97 1.1 christos }
98 1.1 christos
99 1.1 christos /* Just return TRUE. */
100 1.1 christos
101 1.1 christos bfd_boolean
102 1.1 christos generic_symbol_is_valid (asymbol * sym ATTRIBUTE_UNUSED,
103 1.1 christos struct disassemble_info *info ATTRIBUTE_UNUSED)
104 1.1 christos {
105 1.1 christos return TRUE;
106 1.1 christos }
107