2 * Copyright (C) 2012-2019 Free Software Foundation, Inc.
4 * This file is part of GNU lightning.
6 * GNU lightning is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; either version 3, or (at your option)
11 * GNU lightning is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 * License for more details.
17 * Paulo Cesar Pereira de Andrade
20 #include <lightning.h>
21 #include <lightning/jit_private.h>
31 disasm_compare_symbols(const void *ap, const void *bp);
34 disasm_print_address(bfd_vma addr, struct disassemble_info *info);
36 #define disassemble(u, v) _disassemble(_jit, u, v)
38 _disassemble(jit_state_t *_jit, jit_pointer_t code, jit_int32_t length);
45 static bfd *disasm_bfd;
46 static disassemble_info disasm_info;
47 static disassembler_ftype disasm_print;
48 static asymbol **disasm_symbols;
49 static asymbol *disasm_synthetic;
50 static long disasm_num_symbols;
51 static long disasm_num_synthetic;
52 static jit_state_t *disasm_jit;
53 static FILE *disasm_stream;
60 jit_init_debug(const char *progname)
67 disasm_bfd = bfd_openr(progname, NULL);
68 if (disasm_bfd == NULL) {
69 #if defined(__linux__)
70 disasm_bfd = bfd_openr("/proc/self/exe", NULL);
71 if (disasm_bfd == NULL)
75 bfd_check_format(disasm_bfd, bfd_object);
76 bfd_check_format(disasm_bfd, bfd_archive);
78 disasm_stream = stdout;
80 INIT_DISASSEMBLE_INFO(disasm_info, disasm_stream, fprintf);
81 disasm_info.arch = bfd_get_arch(disasm_bfd);
82 disasm_info.mach = bfd_get_mach(disasm_bfd);
84 # if HAVE_DISASSEMBLE_INIT_FOR_TARGET
85 disassemble_init_for_target(&disasm_info);
88 # if defined(__powerpc64__)
89 disasm_info.disassembler_options = "64";
91 # if defined(__sparc__) || defined(__s390__) || defined(__s390x__)
92 disasm_info.endian = disasm_info.display_endian = BFD_ENDIAN_BIG;
94 # if defined(__s390__) || defined(__s390x__)
95 disasm_info.disassembler_options = "zarch";
97 disasm_info.print_address_func = disasm_print_address;
100 disasm_print = disassembler(disasm_info.arch, __BYTE_ORDER == __BIG_ENDIAN,
101 disasm_info.mach, disasm_bfd);
103 disasm_print = disassembler(disasm_bfd);
105 assert(disasm_print);
107 if (bfd_get_file_flags(disasm_bfd) & HAS_SYMS) {
117 if ((sym_storage = bfd_get_symtab_upper_bound(disasm_bfd)) >= 0) {
119 if (bfd_get_file_flags(disasm_bfd) & DYNAMIC) {
120 dyn_storage = bfd_get_dynamic_symtab_upper_bound(disasm_bfd);
121 # if defined(__alpha__)
126 assert(dyn_storage >= 0);
132 jit_alloc((jit_pointer_t *)&disasm_symbols,
133 (sym_storage + dyn_storage) * sizeof(asymbol *));
134 sym_count = bfd_canonicalize_symtab(disasm_bfd, disasm_symbols);
135 assert(sym_count >= 0);
137 dyn_count = bfd_canonicalize_dynamic_symtab(disasm_bfd,
140 assert(dyn_count >= 0);
144 disasm_num_symbols = sym_count + dyn_count;
146 disasm_num_synthetic = bfd_get_synthetic_symtab(disasm_bfd,
153 if (disasm_num_synthetic > 0) {
154 jit_realloc((jit_pointer_t *)&disasm_symbols,
155 (sym_storage + dyn_storage) * sizeof(asymbol *),
156 (sym_storage + dyn_storage + disasm_num_synthetic) *
158 for (offset = 0; offset < disasm_num_synthetic; offset++)
159 disasm_symbols[disasm_num_symbols++] =
160 disasm_synthetic + offset;
163 /* remove symbols not useful for disassemble */
164 in = out = disasm_symbols;
165 for (offset = 0; offset < disasm_num_symbols; offset++) {
168 symbol->name[0] != '\0' &&
169 !(symbol->flags & (BSF_DEBUGGING | BSF_SECTION_SYM)) &&
170 !bfd_is_und_section(symbol->section) &&
171 !bfd_is_com_section(symbol->section))
174 disasm_num_symbols = out - disasm_symbols;
175 qsort(disasm_symbols, disasm_num_symbols,
176 sizeof(asymbol *), disasm_compare_symbols);
183 jit_finish_debug(void)
186 if (disasm_synthetic)
187 jit_free((jit_pointer_t *)&disasm_synthetic);
189 jit_free((jit_pointer_t *)&disasm_symbols);
191 bfd_close (disasm_bfd);
196 _jit_disassemble(jit_state_t *_jit)
200 # if defined(__arm__)
201 /* FIXME add mapping for prolog switching to arm and possible jump
202 * before first prolog also in arm mode */
203 disasm_info.disassembler_options = jit_cpu.thumb ? "force-thumb" : "";
206 disassemble(_jit->code.ptr, _jit->pc.uc - _jit->code.ptr);
212 /* Based on objdump source */
214 disasm_compare_symbols(const void *ap, const void *bp)
216 const asymbol *a = *(const asymbol **)ap;
217 const asymbol *b = *(const asymbol **)bp;
219 if (bfd_asymbol_value(a) > bfd_asymbol_value(b))
221 if (bfd_asymbol_value(a) < bfd_asymbol_value(b))
227 # define address_buffer_length 16
228 # define address_buffer_format "%llx"
230 # define address_buffer_length 32
231 # define address_buffer_format "%lx"
234 disasm_print_address(bfd_vma addr, struct disassemble_info *info)
239 char buffer[address_buffer_length];
241 sprintf(buffer, address_buffer_format, (long long)addr);
242 (*info->fprintf_func)(info->stream, "0x%s", buffer);
244 # define _jit disasm_jit
245 # undef jit_pointer_p
246 # define jit_pointer_p(u) \
247 ((u) >= _jit->code.ptr && (u) < _jit->pc.uc)
248 if (jit_pointer_p((jit_uint8_t *)(jit_word_t)addr)) {
249 if (jit_get_note((jit_uint8_t *)(jit_word_t)addr, &name, &file, &line))
250 (*info->fprintf_func)(info->stream, " %s:%s:%d",
255 # undef jit_pointer_p
257 else if (disasm_num_symbols) {
264 high = disasm_num_symbols;
266 offset = (low + high) >> 1;
267 symbol = disasm_symbols[offset];
268 if (bfd_asymbol_value(symbol) > addr)
270 else if (bfd_asymbol_value(symbol) < addr)
274 } while (low < high);
276 if (offset >= 0 && offset < disasm_num_symbols) {
277 if (bfd_asymbol_value(symbol) < addr) {
278 while (++offset < disasm_num_symbols) {
279 symbol = disasm_symbols[offset];
280 if (bfd_asymbol_value(symbol) >= addr)
284 else if (bfd_asymbol_value(symbol) > addr) {
286 if (bfd_asymbol_value(disasm_symbols[offset]) < addr)
288 symbol = disasm_symbols[offset];
291 if (bfd_asymbol_value(symbol) == addr)
292 (*info->fprintf_func)(info->stream, " # %s", symbol->name);
298 _disassemble(jit_state_t *_jit, jit_pointer_t code, jit_int32_t length)
301 char *name, *old_name;
302 char *file, *old_file;
306 jit_bool_t data_info;
307 jit_int32_t data_offset;
309 bfd_vma pc = (jit_uword_t)code;
310 bfd_vma end = (jit_uword_t)code + length;
311 char buffer[address_buffer_length];
312 #if DEVEL_DISASSEMBLER
318 data_info = _jitc && _jitc->data_info.ptr;
321 disasm_info.buffer = code;
322 disasm_info.buffer_vma = (jit_uword_t)code;
323 disasm_info.buffer_length = length;
324 old_file = old_name = NULL;
327 #if DEVEL_DISASSEMBLER
332 #if DEVEL_DISASSEMBLER
333 while (node && (jit_uword_t)(prevw + node->offset) < (jit_uword_t)pc) {
334 prevw += node->offset;
337 while (node && (jit_uword_t)(prevw + node->offset) == (jit_uword_t)pc) {
338 jit_print_node(node);
340 prevw += node->offset;
347 while (_jitc->data_info.ptr[data_offset].code < pc) {
348 if (++data_offset >= _jitc->data_info.length) {
353 if (pc == _jitc->data_info.ptr[data_offset].code) {
354 offset = _jitc->data_info.ptr[data_offset].length;
355 for (; offset >= 4; offset -= 4, pc += 4) {
356 bytes = sprintf(buffer, address_buffer_format, pc);
357 (*disasm_info.fprintf_func)(disasm_stream,
358 "%*c0x%s\t.data\t0x%08x\n",
359 16 - bytes, ' ', buffer,
363 /* reset disassemble information instead of attempting
364 * to hack the arm specific backend data structures to
365 * tell it to forward the required number of bytes. */
366 disasm_info.buffer = (jit_pointer_t)(jit_uint32_t)pc;
367 disasm_info.buffer_vma = (jit_uword_t)pc;
368 if ((disasm_info.buffer_length = end - pc) <= 0)
373 if (jit_get_note((jit_uint8_t *)(jit_word_t)pc, &name, &file, &line) &&
374 (name != old_name || file != old_file || line != old_line)) {
375 (*disasm_info.fprintf_func)(disasm_stream, "# %s:%s:%d\n",
384 bytes = sprintf(buffer, address_buffer_format, (long long)pc);
385 (*disasm_info.fprintf_func)(disasm_stream, "%*c0x%s\t",
386 16 - bytes, ' ', buffer);
387 pc += (*disasm_print)(pc, &disasm_info);
388 putc('\n', disasm_stream);