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 #define disasm_stream stdout
60 jit_init_debug(const char *progname)
66 disasm_bfd = bfd_openr(progname, NULL);
67 if (disasm_bfd == NULL) {
68 #if defined(__linux__)
69 disasm_bfd = bfd_openr("/proc/self/exe", NULL);
70 if (disasm_bfd == NULL)
74 bfd_check_format(disasm_bfd, bfd_object);
75 bfd_check_format(disasm_bfd, bfd_archive);
76 INIT_DISASSEMBLE_INFO(disasm_info, disasm_stream, fprintf);
77 # if defined(__i386__) || defined(__x86_64__)
78 disasm_info.arch = bfd_arch_i386;
79 # if defined(__x86_64__)
81 disasm_info.mach = bfd_mach_x64_32;
83 disasm_info.mach = bfd_mach_x86_64;
86 disasm_info.mach = bfd_mach_i386_i386;
89 # if defined(__powerpc__)
90 disasm_info.arch = bfd_arch_powerpc;
91 disasm_info.mach = bfd_mach_ppc64;
92 # if HAVE_DISASSEMBLE_INIT_FOR_TARGET
93 disassemble_init_for_target(&disasm_info);
94 # elif HAVE_DISASSEMBLE_INIT_POWERPC
95 disassemble_init_powerpc(&disasm_info);
97 # if defined(__powerpc64__)
98 disasm_info.disassembler_options = "64";
100 # if HAVE_DISASSEMBLE_INIT_FOR_TARGET
101 disassemble_init_for_target(&disasm_info);
102 # elif HAVE_DISASSEMBLE_INIT_POWERPC
103 disassemble_init_powerpc(&disasm_info);
106 # if defined(__sparc__)
107 disasm_info.endian = disasm_info.display_endian = BFD_ENDIAN_BIG;
109 # if defined(__s390__) || defined(__s390x__)
110 disasm_info.arch = bfd_arch_s390;
111 # if __WORDSIZE == 32
112 disasm_info.mach = bfd_mach_s390_31;
114 disasm_info.mach = bfd_mach_s390_64;
116 disasm_info.endian = disasm_info.display_endian = BFD_ENDIAN_BIG;
117 disasm_info.disassembler_options = "zarch";
119 # if defined(__alpha__)
120 disasm_info.arch = bfd_arch_alpha;
121 disasm_info.mach = bfd_mach_alpha_ev6;
123 # if defined(__hppa__)
124 disasm_info.arch = bfd_arch_hppa;
125 disasm_info.mach = bfd_mach_hppa10;
127 # if defined(__riscv)
128 disasm_info.arch = bfd_arch_riscv;
129 # if __WORDSIZE == 32
130 disasm_info.mach = bfd_mach_riscv32;
132 disasm_info.mach = bfd_mach_riscv64;
135 disasm_info.print_address_func = disasm_print_address;
138 disasm_print = disassembler(disasm_info.arch, __BYTE_ORDER == __BIG_ENDIAN,
139 disasm_info.mach, disasm_bfd);
141 disasm_print = disassembler(disasm_bfd);
143 assert(disasm_print);
145 if (bfd_get_file_flags(disasm_bfd) & HAS_SYMS) {
155 if ((sym_storage = bfd_get_symtab_upper_bound(disasm_bfd)) >= 0) {
157 if (bfd_get_file_flags(disasm_bfd) & DYNAMIC) {
158 dyn_storage = bfd_get_dynamic_symtab_upper_bound(disasm_bfd);
159 # if defined(__alpha__)
164 assert(dyn_storage >= 0);
170 jit_alloc((jit_pointer_t *)&disasm_symbols,
171 (sym_storage + dyn_storage) * sizeof(asymbol *));
172 sym_count = bfd_canonicalize_symtab(disasm_bfd, disasm_symbols);
173 assert(sym_count >= 0);
175 dyn_count = bfd_canonicalize_dynamic_symtab(disasm_bfd,
178 assert(dyn_count >= 0);
182 disasm_num_symbols = sym_count + dyn_count;
184 disasm_num_synthetic = bfd_get_synthetic_symtab(disasm_bfd,
191 if (disasm_num_synthetic > 0) {
192 jit_realloc((jit_pointer_t *)&disasm_symbols,
193 (sym_storage + dyn_storage) * sizeof(asymbol *),
194 (sym_storage + dyn_storage + disasm_num_synthetic) *
196 for (offset = 0; offset < disasm_num_synthetic; offset++)
197 disasm_symbols[disasm_num_symbols++] =
198 disasm_synthetic + offset;
201 /* remove symbols not useful for disassemble */
202 in = out = disasm_symbols;
203 for (offset = 0; offset < disasm_num_symbols; offset++) {
206 symbol->name[0] != '\0' &&
207 !(symbol->flags & (BSF_DEBUGGING | BSF_SECTION_SYM)) &&
208 !bfd_is_und_section(symbol->section) &&
209 !bfd_is_com_section(symbol->section))
212 disasm_num_symbols = out - disasm_symbols;
213 qsort(disasm_symbols, disasm_num_symbols,
214 sizeof(asymbol *), disasm_compare_symbols);
221 jit_finish_debug(void)
224 if (disasm_synthetic)
225 jit_free((jit_pointer_t *)&disasm_synthetic);
227 jit_free((jit_pointer_t *)&disasm_symbols);
229 bfd_close (disasm_bfd);
234 _jit_disassemble(jit_state_t *_jit)
238 # if defined(__arm__)
239 /* FIXME add mapping for prolog switching to arm and possible jump
240 * before first prolog also in arm mode */
241 disasm_info.disassembler_options = jit_cpu.thumb ? "force-thumb" : "";
244 disassemble(_jit->code.ptr, _jit->pc.uc - _jit->code.ptr);
250 /* Based on objdump source */
252 disasm_compare_symbols(const void *ap, const void *bp)
254 const asymbol *a = *(const asymbol **)ap;
255 const asymbol *b = *(const asymbol **)bp;
257 if (bfd_asymbol_value(a) > bfd_asymbol_value(b))
259 if (bfd_asymbol_value(a) < bfd_asymbol_value(b))
265 # define address_buffer_length 16
266 # define address_buffer_format "%llx"
268 # define address_buffer_length 32
269 # define address_buffer_format "%lx"
272 disasm_print_address(bfd_vma addr, struct disassemble_info *info)
277 char buffer[address_buffer_length];
279 sprintf(buffer, address_buffer_format, (long long)addr);
280 (*info->fprintf_func)(info->stream, "0x%s", buffer);
282 # define _jit disasm_jit
283 # undef jit_pointer_p
284 # define jit_pointer_p(u) \
285 ((u) >= _jit->code.ptr && (u) < _jit->pc.uc)
286 if (jit_pointer_p((jit_uint8_t *)(jit_word_t)addr)) {
287 if (jit_get_note((jit_uint8_t *)(jit_word_t)addr, &name, &file, &line))
288 (*info->fprintf_func)(info->stream, " %s:%s:%d",
293 # undef jit_pointer_p
295 else if (disasm_num_symbols) {
302 high = disasm_num_symbols;
304 offset = (low + high) >> 1;
305 symbol = disasm_symbols[offset];
306 if (bfd_asymbol_value(symbol) > addr)
308 else if (bfd_asymbol_value(symbol) < addr)
312 } while (low < high);
314 if (offset >= 0 && offset < disasm_num_symbols) {
315 if (bfd_asymbol_value(symbol) < addr) {
316 while (++offset < disasm_num_symbols) {
317 symbol = disasm_symbols[offset];
318 if (bfd_asymbol_value(symbol) >= addr)
322 else if (bfd_asymbol_value(symbol) > addr) {
324 if (bfd_asymbol_value(disasm_symbols[offset]) < addr)
326 symbol = disasm_symbols[offset];
329 if (bfd_asymbol_value(symbol) == addr)
330 (*info->fprintf_func)(info->stream, " # %s", symbol->name);
336 _disassemble(jit_state_t *_jit, jit_pointer_t code, jit_int32_t length)
339 char *name, *old_name;
340 char *file, *old_file;
344 jit_bool_t data_info;
345 jit_int32_t data_offset;
347 bfd_vma pc = (jit_uword_t)code;
348 bfd_vma end = (jit_uword_t)code + length;
349 char buffer[address_buffer_length];
350 #if DEVEL_DISASSEMBLER
356 data_info = _jitc && _jitc->data_info.ptr;
359 disasm_info.buffer = code;
360 disasm_info.buffer_vma = (jit_uword_t)code;
361 disasm_info.buffer_length = length;
362 old_file = old_name = NULL;
365 #if DEVEL_DISASSEMBLER
370 #if DEVEL_DISASSEMBLER
371 while (node && (jit_uword_t)(prevw + node->offset) < (jit_uword_t)pc) {
372 prevw += node->offset;
375 while (node && (jit_uword_t)(prevw + node->offset) == (jit_uword_t)pc) {
376 jit_print_node(node);
378 prevw += node->offset;
385 while (_jitc->data_info.ptr[data_offset].code < pc) {
386 if (++data_offset >= _jitc->data_info.length) {
391 if (pc == _jitc->data_info.ptr[data_offset].code) {
392 offset = _jitc->data_info.ptr[data_offset].length;
393 for (; offset >= 4; offset -= 4, pc += 4) {
394 bytes = sprintf(buffer, address_buffer_format, pc);
395 (*disasm_info.fprintf_func)(disasm_stream,
396 "%*c0x%s\t.data\t0x%08x\n",
397 16 - bytes, ' ', buffer,
401 /* reset disassemble information instead of attempting
402 * to hack the arm specific backend data structures to
403 * tell it to forward the required number of bytes. */
404 disasm_info.buffer = (jit_pointer_t)(jit_uint32_t)pc;
405 disasm_info.buffer_vma = (jit_uword_t)pc;
406 if ((disasm_info.buffer_length = end - pc) <= 0)
411 if (jit_get_note((jit_uint8_t *)(jit_word_t)pc, &name, &file, &line) &&
412 (name != old_name || file != old_file || line != old_line)) {
413 (*disasm_info.fprintf_func)(disasm_stream, "# %s:%s:%d\n",
422 bytes = sprintf(buffer, address_buffer_format, (long long)pc);
423 (*disasm_info.fprintf_func)(disasm_stream, "%*c0x%s\t",
424 16 - bytes, ' ', buffer);
425 pc += (*disasm_print)(pc, &disasm_info);
426 putc('\n', disasm_stream);