ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/mon/src/mon_disass.cpp
Revision: 1.11
Committed: 2007-06-07T09:51:56Z (17 years, 5 months ago) by gbeauche
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +24 -0 lines
Log Message:
Add MIPS disassembler invoked as "dm" and "dmel" (little-endian) for now.

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * mon_disass.cpp - Disassemblers
3     *
4 cebix 1.9 * cxmon (C) 1997-2004 Christian Bauer, Marc Hellwig
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #include "sysdeps.h"
22    
23 gbeauche 1.11 #include <assert.h>
24 cebix 1.1 #include <stdarg.h>
25    
26     #include "mon.h"
27     #include "mon_disass.h"
28 cebix 1.2
29     #include "mon_atraps.h"
30 cebix 1.1 #include "mon_lowmem.h"
31    
32    
33 cebix 1.2 // Flag: enable MacOS A-Trap and LM globals lookup in 68k disassembler
34     bool mon_macos_mode = false;
35    
36    
37 cebix 1.1 /*
38     * GNU disassembler callbacks
39     */
40    
41     extern "C" {
42     #include "disass/dis-asm.h"
43    
44 gbeauche 1.6 int buffer_read_memory(bfd_vma from, bfd_byte *to, unsigned int length, struct disassemble_info *info)
45 cebix 1.1 {
46     while (length--)
47     *to++ = mon_read_byte(from++);
48     return 0;
49     }
50    
51     void perror_memory(int status, bfd_vma memaddr, struct disassemble_info *info)
52     {
53     info->fprintf_func(info->stream, "Unknown error %d\n", status);
54     }
55    
56     bool lookup_lowmem;
57    
58     void generic_print_address(bfd_vma addr, struct disassemble_info *info)
59     {
60 cebix 1.2 if (lookup_lowmem && addr >= 0x100 && addr < 0x3000) {
61     if (((addr >= 0x400 && addr < 0x800) || (addr >= 0xe00 && addr < 0x1e00)) && ((addr & 3) == 0)) {
62     // Look for address in A-Trap table
63     uint16 opcode = (addr < 0xe00 ? 0xa000 + (addr - 0x400) / 4 : 0xa800 + (addr - 0xe00) / 4);
64     uint16 mask = (addr < 0xe00 ? 0xf8ff : 0xffff);
65     const atrap_info *p = atraps;
66     while (p->word) {
67     if ((p->word & mask) == opcode) {
68     info->fprintf_func(info->stream, p->name);
69     return;
70     }
71     p++;
72     }
73     } else {
74     // Look for address in low memory globals table
75     const lowmem_info *p = lowmem;
76     while (p->name) {
77     if (addr >= p[0].addr && addr < p[1].addr) {
78     if (addr == p[0].addr)
79     info->fprintf_func(info->stream, "%s", p->name);
80     else
81     info->fprintf_func(info->stream, "%s+%d", p->name, addr - p->addr);
82     return;
83     }
84     p++;
85 cebix 1.1 }
86     }
87     }
88 gbeauche 1.10 if (addr >= UVAL64(0x100000000))
89     info->fprintf_func(info->stream, "$%08x%08x", (uint32)(addr >> 32), (uint32)addr);
90     else
91     info->fprintf_func(info->stream, "$%08x", (uint32)addr);
92 cebix 1.1 }
93    
94     int generic_symbol_at_address(bfd_vma addr, struct disassemble_info *info)
95     {
96     return 0;
97     }
98    
99 cebix 1.2 void print_68k_invalid_opcode(unsigned long opcode, struct disassemble_info *info)
100     {
101     if (mon_macos_mode) {
102     // Look for MacOS A-Trap
103     const atrap_info *p = atraps;
104     while (p->word) {
105     if (p->word == opcode) {
106     info->fprintf_func(info->stream, p->name);
107     return;
108     }
109     p++;
110     }
111     }
112     info->fprintf_func(info->stream, "?");
113     }
114    
115 cebix 1.1 };
116    
117    
118     /*
119     * sprintf into a "stream"
120     */
121    
122     struct SFILE {
123     char *buffer;
124     char *current;
125     };
126    
127     static int mon_sprintf(SFILE *f, const char *format, ...)
128     {
129     int n;
130     va_list args;
131     va_start(args, format);
132     vsprintf(f->current, format, args);
133     f->current += n = strlen(f->current);
134     va_end(args);
135     return n;
136     }
137    
138    
139     /*
140     * Disassemble one instruction, return number of bytes
141     */
142    
143     int disass_68k(FILE *f, uint32 adr)
144     {
145     // Initialize info for GDB disassembler
146     disassemble_info info;
147     char buf[1024];
148     SFILE sfile = {buf, buf};
149     sfile.buffer = buf;
150     sfile.current = buf;
151     INIT_DISASSEMBLE_INFO(info, (FILE *)&sfile, (fprintf_ftype)mon_sprintf);
152    
153     // Disassemble instruction
154 cebix 1.2 lookup_lowmem = mon_macos_mode;
155 cebix 1.1 int num = print_insn_m68k(adr, &info);
156 cebix 1.2
157     for (int i=0; i<6; i+=2) {
158     if (num > i)
159     fprintf(f, "%04x ", mon_read_half(adr + i));
160     else
161     fprintf(f, " ");
162     }
163 cebix 1.1 if (num == 8)
164     fprintf(f, "%04x\t%s\n", mon_read_half(adr + 6), buf);
165 cebix 1.2 else if (num > 8)
166 cebix 1.1 fprintf(f, "...\t%s\n", buf);
167     else
168     fprintf(f, " \t%s\n", buf);
169 cebix 1.2
170 cebix 1.1 return num;
171     }
172    
173 gbeauche 1.11 int disass_mips(FILE *f, uint32 adr, int little_endian)
174     {
175     // Initialize info for GDB disassembler
176     disassemble_info info;
177     char buf[1024];
178     SFILE sfile = {buf, buf};
179     sfile.buffer = buf;
180     sfile.current = buf;
181     INIT_DISASSEMBLE_INFO(info, (FILE *)&sfile, (fprintf_ftype)mon_sprintf);
182    
183     // Disassemble instruction
184     lookup_lowmem = false;
185     int num = little_endian ? print_insn_little_mips(adr, &info) : print_insn_big_mips(adr, &info);
186    
187     assert(num == 4);
188     for (int i=0; i<4; i++)
189     fprintf(f, " %02x", mon_read_byte(adr + i));
190    
191     fprintf(f, " %s\n", buf);
192    
193     return num;
194     }
195    
196 gbeauche 1.6 int disass_x86(FILE *f, uint32 adr, uint32 bits)
197 cebix 1.1 {
198     // Initialize info for GDB disassembler
199     disassemble_info info;
200     char buf[1024];
201     SFILE sfile = {buf, buf};
202     sfile.buffer = buf;
203     sfile.current = buf;
204     INIT_DISASSEMBLE_INFO(info, (FILE *)&sfile, (fprintf_ftype)mon_sprintf);
205 gbeauche 1.6 if (bits == 16)
206 cebix 1.3 info.mach = bfd_mach_i386_i8086;
207 gbeauche 1.6 else if (bits == 64)
208     info.mach = bfd_mach_x86_64;
209 cebix 1.1
210     // Disassemble instruction
211     lookup_lowmem = false;
212 gbeauche 1.7 int num = print_insn_i386_att(adr, &info);
213 cebix 1.2
214     for (int i=0; i<6; i++) {
215     if (num > i)
216     fprintf(f, "%02x ", mon_read_byte(adr + i));
217     else
218     fprintf(f, " ");
219     }
220     if (num == 7)
221     fprintf(f, "%02x\t%s\n", mon_read_byte(adr + 7), buf);
222     else if (num > 7)
223     fprintf(f, "..\t%s\n", buf);
224     else
225     fprintf(f, " \t%s\n", buf);
226    
227 cebix 1.1 return num;
228     }