1 |
/* |
2 |
* mon_disass.cpp - Disassemblers |
3 |
* |
4 |
* mon (C) 1997-2000 Christian Bauer, Marc Hellwig |
5 |
* |
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 |
#include <stdarg.h> |
24 |
|
25 |
#include "mon.h" |
26 |
#include "mon_disass.h" |
27 |
#include "mon_lowmem.h" |
28 |
|
29 |
|
30 |
/* |
31 |
* GNU disassembler callbacks |
32 |
*/ |
33 |
|
34 |
extern "C" { |
35 |
#include "disass/dis-asm.h" |
36 |
|
37 |
int buffer_read_memory(bfd_vma from, bfd_byte *to, int length, struct disassemble_info *info) |
38 |
{ |
39 |
while (length--) |
40 |
*to++ = mon_read_byte(from++); |
41 |
return 0; |
42 |
} |
43 |
|
44 |
void perror_memory(int status, bfd_vma memaddr, struct disassemble_info *info) |
45 |
{ |
46 |
info->fprintf_func(info->stream, "Unknown error %d\n", status); |
47 |
} |
48 |
|
49 |
bool lookup_lowmem; |
50 |
|
51 |
void generic_print_address(bfd_vma addr, struct disassemble_info *info) |
52 |
{ |
53 |
if (lookup_lowmem && ((addr >= 0x100 && addr < 0x400) || (addr >= 0x800 && addr < 0xe00) || (addr >= 0x1e00 && addr < 0x3000))) { |
54 |
// Look for address in low memory globals table |
55 |
const lowmem_info *p = lowmem; |
56 |
while (p->name) { |
57 |
if (addr >= p[0].addr && addr < p[1].addr) { |
58 |
if (addr == p[0].addr) |
59 |
info->fprintf_func(info->stream, "%s", p->name); |
60 |
else |
61 |
info->fprintf_func(info->stream, "%s+%d", p->name, addr - p->addr); |
62 |
return; |
63 |
} |
64 |
p++; |
65 |
} |
66 |
} |
67 |
info->fprintf_func(info->stream, "$%08x", addr); |
68 |
} |
69 |
|
70 |
int generic_symbol_at_address(bfd_vma addr, struct disassemble_info *info) |
71 |
{ |
72 |
return 0; |
73 |
} |
74 |
|
75 |
}; |
76 |
|
77 |
|
78 |
/* |
79 |
* sprintf into a "stream" |
80 |
*/ |
81 |
|
82 |
struct SFILE { |
83 |
char *buffer; |
84 |
char *current; |
85 |
}; |
86 |
|
87 |
static int mon_sprintf(SFILE *f, const char *format, ...) |
88 |
{ |
89 |
int n; |
90 |
va_list args; |
91 |
va_start(args, format); |
92 |
vsprintf(f->current, format, args); |
93 |
f->current += n = strlen(f->current); |
94 |
va_end(args); |
95 |
return n; |
96 |
} |
97 |
|
98 |
|
99 |
/* |
100 |
* Disassemble one instruction, return number of bytes |
101 |
*/ |
102 |
|
103 |
int disass_68k(FILE *f, uint32 adr) |
104 |
{ |
105 |
// Initialize info for GDB disassembler |
106 |
disassemble_info info; |
107 |
char buf[1024]; |
108 |
SFILE sfile = {buf, buf}; |
109 |
sfile.buffer = buf; |
110 |
sfile.current = buf; |
111 |
INIT_DISASSEMBLE_INFO(info, (FILE *)&sfile, (fprintf_ftype)mon_sprintf); |
112 |
|
113 |
// Disassemble instruction |
114 |
lookup_lowmem = true; |
115 |
int num = print_insn_m68k(adr, &info); |
116 |
if (num >= 2) |
117 |
fprintf(f, "%04x ", mon_read_half(adr)); |
118 |
else |
119 |
fprintf(f, " "); |
120 |
if (num >= 4) |
121 |
fprintf(f, "%04x ", mon_read_half(adr + 2)); |
122 |
else |
123 |
fprintf(f, " "); |
124 |
if (num >= 6) |
125 |
fprintf(f, "%04x ", mon_read_half(adr + 4)); |
126 |
else |
127 |
fprintf(f, " "); |
128 |
if (num == 8) |
129 |
fprintf(f, "%04x\t%s\n", mon_read_half(adr + 6), buf); |
130 |
else if (num >= 8) |
131 |
fprintf(f, "...\t%s\n", buf); |
132 |
else |
133 |
fprintf(f, " \t%s\n", buf); |
134 |
return num; |
135 |
} |
136 |
|
137 |
int disass_x86(FILE *f, uint32 adr) |
138 |
{ |
139 |
// Initialize info for GDB disassembler |
140 |
disassemble_info info; |
141 |
char buf[1024]; |
142 |
SFILE sfile = {buf, buf}; |
143 |
sfile.buffer = buf; |
144 |
sfile.current = buf; |
145 |
INIT_DISASSEMBLE_INFO(info, (FILE *)&sfile, (fprintf_ftype)mon_sprintf); |
146 |
|
147 |
// Disassemble instruction |
148 |
lookup_lowmem = false; |
149 |
int num = print_insn_i386(adr, &info); |
150 |
fprintf(f, "%s\n", buf); |
151 |
return num; |
152 |
} |