15597b4647f1f2c560ce2dc1de263dce96a9dab2
[libpicofe.git] / linux / host_dasm.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
3  *
4  * This work is licensed under the terms of any of these licenses
5  * (at your option):
6  *  - GNU GPL, version 2 or later.
7  *  - GNU LGPL, version 2.1 or later.
8  *  - MAME license.
9  * See the COPYING file in the top-level directory.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <stdarg.h>
16 #include <bfd.h>
17 #include <dis-asm.h>
18
19 #include "host_dasm.h"
20
21 extern char **g_argv;
22
23 static struct disassemble_info di;
24
25 #ifdef __arm__
26 #define print_insn_func print_insn_little_arm
27 #define BFD_ARCH bfd_arch_arm
28 #define BFD_MACH bfd_mach_arm_unknown
29 #define DASM_OPTS "reg-names-std"
30 #else
31 #define print_insn_func print_insn_i386_intel
32 #define BFD_ARCH bfd_arch_i386
33 #define BFD_MACH bfd_mach_i386_i386_intel_syntax
34 #define DASM_OPTS NULL
35 #endif
36
37 /* symbols */
38 static asymbol **symbols;
39 static long symcount, symstorage;
40 static int init_done;
41
42 /* Filter out (in place) symbols that are useless for disassembly.
43    COUNT is the number of elements in SYMBOLS.
44    Return the number of useful symbols.  */
45 static long
46 remove_useless_symbols (asymbol **symbols, long count)
47 {
48   asymbol **in_ptr = symbols, **out_ptr = symbols;
49
50   while (--count >= 0)
51     {
52       asymbol *sym = *in_ptr++;
53
54       if (sym->name == NULL || sym->name[0] == '\0' || sym->name[0] == '$')
55         continue;
56       if (sym->flags & (BSF_DEBUGGING | BSF_SECTION_SYM))
57         continue;
58       if (bfd_is_und_section (sym->section)
59           || bfd_is_com_section (sym->section))
60         continue;
61       if (sym->value + sym->section->vma == 0)
62         continue;
63 /*
64       printf("sym: %08lx %04x %08x v %08x \"%s\"\n",
65         (unsigned int)sym->value, (unsigned int)sym->flags, (unsigned int)sym->udata.i,
66         (unsigned int)sym->section->vma, sym->name);
67 */
68       *out_ptr++ = sym;
69     }
70
71   return out_ptr - symbols;
72 }
73
74 static void slurp_symtab(const char *filename)
75 {
76   bfd *abfd;
77
78   symcount = 0;
79
80   abfd = bfd_openr(filename, NULL);
81   if (abfd == NULL) {
82     fprintf(stderr, "failed to open: %s\n", filename);
83     goto no_symbols;
84   }
85
86   if (!bfd_check_format(abfd, bfd_object))
87     goto no_symbols;
88
89   if (!(bfd_get_file_flags(abfd) & HAS_SYMS))
90     goto no_symbols;
91
92   symstorage = bfd_get_symtab_upper_bound(abfd);
93   if (symstorage <= 0)
94     goto no_symbols;
95
96   symbols = malloc(symstorage);
97   if (symbols == NULL)
98     goto no_symbols;
99
100   symcount = bfd_canonicalize_symtab(abfd, symbols);
101   if (symcount < 0)
102     goto no_symbols;
103
104   symcount = remove_useless_symbols(symbols, symcount);
105 //  bfd_close(abfd);
106   return;
107
108 no_symbols:
109   fprintf(stderr, "no symbols in %s\n", bfd_get_filename(abfd));
110   if (symbols != NULL)
111     free(symbols);
112   symbols = NULL;
113   if (abfd != NULL)
114     bfd_close(abfd);
115 }
116
117 static const char *lookup_name(bfd_vma addr)
118 {
119   asymbol **sptr = symbols;
120   int i;
121
122   for (i = 0; i < symcount; i++) {
123     asymbol *sym = *sptr++;
124
125     if (addr == sym->value + sym->section->vma)
126       return sym->name;
127   }
128
129   return NULL;
130 }
131
132 /* Like target_read_memory, but slightly different parameters.  */
133 static int
134 dis_asm_read_memory(bfd_vma memaddr, bfd_byte *myaddr, unsigned int len,
135                      struct disassemble_info *info)
136 {
137   memcpy(myaddr, (void *)(int)memaddr, len);
138   return 0;
139 }
140
141 static void
142 dis_asm_memory_error(int status, bfd_vma memaddr,
143                       struct disassemble_info *info)
144 {
145   fprintf(stderr, "memory_error %p\n", (void *)(int)memaddr);
146 }
147
148 static void
149 dis_asm_print_address(bfd_vma addr, struct disassemble_info *info)
150 {
151   const char *name;
152
153   printf("%08x", (int)addr);
154
155   name = lookup_name(addr);
156   if (name != NULL)
157     printf(" <%s>", name);
158 }
159
160 static int insn_printf(void *f, const char *format, ...)
161 {
162   va_list args;
163   size_t n;
164
165   va_start(args, format);
166   n = vprintf(format, args);
167   va_end(args);
168
169   return n;
170 }
171
172 static void host_dasm_init(void)
173 {
174   bfd_init();
175   slurp_symtab(g_argv[0]);
176
177   init_disassemble_info(&di, NULL, insn_printf);
178   di.flavour = bfd_target_unknown_flavour;
179   di.memory_error_func = dis_asm_memory_error; 
180   di.print_address_func = dis_asm_print_address;
181 //  di.symbol_at_address_func = dis_asm_symbol_at_address;
182   di.read_memory_func = dis_asm_read_memory;
183   di.arch = BFD_ARCH;
184   di.mach = BFD_MACH;
185   di.endian = BFD_ENDIAN_LITTLE;
186   di.disassembler_options = DASM_OPTS;
187   disassemble_init_for_target(&di);
188   init_done = 1;
189 }
190
191 void host_dasm(void *addr, int len)
192 {
193   bfd_vma vma_end, vma = (bfd_vma)(long)addr;
194   const char *name;
195
196   if (!init_done)
197     host_dasm_init();
198
199   vma_end = vma + len;
200   while (vma < vma_end) {
201     name = lookup_name(vma);
202     if (name != NULL)
203       printf("%s:\n", name);
204
205     printf("   %08lx ", (long)vma);
206     vma += print_insn_func(vma, &di);
207     printf("\n");
208   }
209 }
210
211 void host_dasm_new_symbol_(void *addr, const char *name)
212 {
213   bfd_vma vma = (bfd_vma)(long)addr;
214   asymbol *sym, **tmp;
215
216   if (!init_done)
217     host_dasm_init();
218   if (symbols == NULL)
219     return;
220   if (symstorage <= symcount * sizeof(symbols[0])) {
221     tmp = realloc(symbols, symstorage * 2);
222     if (tmp == NULL)
223       return;
224     symstorage *= 2;
225     symbols = tmp;
226   }
227
228   symbols[symcount] = calloc(sizeof(*symbols[0]), 1);
229   if (symbols[symcount] == NULL)
230     return;
231
232   // a HACK (should use correct section), but ohwell
233   sym = symbols[symcount];
234   sym->section = symbols[0]->section;
235   sym->value = vma - sym->section->vma;
236   sym->name = name;
237   symcount++;
238 }
239