2004-03-17 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / opcodes / dis-buf.c
blob83fbfbd7d21e62aac239d1453a73bf816334dde9
1 /* Disassemble from a buffer, for GNU.
2 Copyright 1993, 1994, 1996, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #include "sysdep.h"
20 #include "dis-asm.h"
21 #include <errno.h>
22 #include "opintl.h"
24 /* Get LENGTH bytes from info's buffer, at target address memaddr.
25 Transfer them to myaddr. */
26 int
27 buffer_read_memory (memaddr, myaddr, length, info)
28 bfd_vma memaddr;
29 bfd_byte *myaddr;
30 unsigned int length;
31 struct disassemble_info *info;
33 unsigned int opb = info->octets_per_byte;
34 unsigned int end_addr_offset = length / opb;
35 unsigned int max_addr_offset = info->buffer_length / opb;
36 unsigned int octets = (memaddr - info->buffer_vma) * opb;
38 if (memaddr < info->buffer_vma
39 || memaddr - info->buffer_vma + end_addr_offset > max_addr_offset)
40 /* Out of bounds. Use EIO because GDB uses it. */
41 return EIO;
42 memcpy (myaddr, info->buffer + octets, length);
44 return 0;
47 /* Print an error message. We can assume that this is in response to
48 an error return from buffer_read_memory. */
49 void
50 perror_memory (status, memaddr, info)
51 int status;
52 bfd_vma memaddr;
53 struct disassemble_info *info;
55 if (status != EIO)
56 /* Can't happen. */
57 info->fprintf_func (info->stream, _("Unknown error %d\n"), status);
58 else
59 /* Actually, address between memaddr and memaddr + len was
60 out of bounds. */
61 info->fprintf_func (info->stream,
62 _("Address 0x%x is out of bounds.\n"), memaddr);
65 /* This could be in a separate file, to save miniscule amounts of space
66 in statically linked executables. */
68 /* Just print the address is hex. This is included for completeness even
69 though both GDB and objdump provide their own (to print symbolic
70 addresses). */
72 void
73 generic_print_address (addr, info)
74 bfd_vma addr;
75 struct disassemble_info *info;
77 char buf[30];
79 sprintf_vma (buf, addr);
80 (*info->fprintf_func) (info->stream, "0x%s", buf);
83 #if 0
84 /* Just concatenate the address as hex. This is included for
85 completeness even though both GDB and objdump provide their own (to
86 print symbolic addresses). */
88 void generic_strcat_address PARAMS ((bfd_vma, char *, int));
90 void
91 generic_strcat_address (addr, buf, len)
92 bfd_vma addr;
93 char *buf;
94 int len;
96 if (buf != (char *)NULL && len > 0)
98 char tmpBuf[30];
100 sprintf_vma (tmpBuf, addr);
101 if ((strlen (buf) + strlen (tmpBuf)) <= (unsigned int) len)
102 strcat (buf, tmpBuf);
103 else
104 strncat (buf, tmpBuf, (len - strlen(buf)));
106 return;
108 #endif
110 /* Just return true. */
113 generic_symbol_at_address (addr, info)
114 bfd_vma addr ATTRIBUTE_UNUSED;
115 struct disassemble_info *info ATTRIBUTE_UNUSED;
117 return 1;
120 /* Just return TRUE. */
122 bfd_boolean
123 generic_symbol_is_valid (asymbol * sym ATTRIBUTE_UNUSED,
124 struct disassemble_info *info ATTRIBUTE_UNUSED)
126 return TRUE;