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