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