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