1 /* Convert a DWARF location expression to C
3 Copyright (C) 2014-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "dwarf2expr.h"
23 #include "dwarf2loc.h"
26 #include "compile-internal.h"
29 #include "dwarf2-frame.h"
35 /* Information about a given instruction. */
39 /* Stack depth at entry. */
43 /* Whether this instruction has been visited. */
45 unsigned int visited : 1;
47 /* Whether this instruction needs a label. */
49 unsigned int label : 1;
51 /* Whether this instruction is DW_OP_GNU_push_tls_address or
52 DW_OP_form_tls_address. This is a hack until we can add a
53 feature to glibc to let us properly generate code for TLS. */
55 unsigned int is_tls : 1;
58 /* A helper function for compute_stack_depth that does the work. This
59 examines the DWARF expression starting from START and computes
62 NEED_TEMPVAR is an out parameter which is set if this expression
63 needs a special temporary variable to be emitted (see the code
65 INFO is an array of insn_info objects, indexed by offset from the
66 start of the DWARF expression.
67 TO_DO is a list of bytecodes which must be examined; it may be
68 added to by this function.
69 BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
70 OP_PTR and OP_END are the bounds of the DWARF expression. */
73 compute_stack_depth_worker (int start, int *need_tempvar,
74 struct insn_info *info,
76 enum bfd_endian byte_order, unsigned int addr_size,
77 const gdb_byte *op_ptr, const gdb_byte *op_end)
79 const gdb_byte * const base = op_ptr;
83 gdb_assert (info[start].visited);
84 stack_depth = info[start].depth;
86 while (op_ptr < op_end)
88 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
91 int ndx = op_ptr - base;
93 #define SET_CHECK_DEPTH(WHERE) \
94 if (info[WHERE].visited) \
96 if (info[WHERE].depth != stack_depth) \
97 error (_("inconsistent stack depths")); \
101 /* Stack depth not set, so set it. */ \
102 info[WHERE].visited = 1; \
103 info[WHERE].depth = stack_depth; \
106 SET_CHECK_DEPTH (ndx);
174 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
214 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
250 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
255 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
256 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
261 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
293 case DW_OP_deref_size:
297 case DW_OP_plus_uconst:
298 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
321 case DW_OP_call_frame_cfa:
325 case DW_OP_GNU_push_tls_address:
326 case DW_OP_form_tls_address:
327 info[ndx].is_tls = 1;
331 offset = extract_signed_integer (op_ptr, 2, byte_order);
333 offset = op_ptr + offset - base;
334 /* If the destination has not been seen yet, add it to the
336 if (!info[offset].visited)
337 VEC_safe_push (int, *to_do, offset);
338 SET_CHECK_DEPTH (offset);
339 info[offset].label = 1;
340 /* We're done with this line of code. */
344 offset = extract_signed_integer (op_ptr, 2, byte_order);
346 offset = op_ptr + offset - base;
348 /* If the destination has not been seen yet, add it to the
350 if (!info[offset].visited)
351 VEC_safe_push (int, *to_do, offset);
352 SET_CHECK_DEPTH (offset);
353 info[offset].label = 1;
360 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
364 gdb_assert (op_ptr == op_end);
366 #undef SET_CHECK_DEPTH
369 /* Compute the maximum needed stack depth of a DWARF expression, and
370 some other information as well.
372 BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
373 NEED_TEMPVAR is an out parameter which is set if this expression
374 needs a special temporary variable to be emitted (see the code
376 IS_TLS is an out parameter which is set if this expression refers
378 OP_PTR and OP_END are the bounds of the DWARF expression.
379 INITIAL_DEPTH is the initial depth of the DWARF expression stack.
380 INFO is an array of insn_info objects, indexed by offset from the
381 start of the DWARF expression.
383 This returns the maximum stack depth. */
386 compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
387 int *need_tempvar, int *is_tls,
388 const gdb_byte *op_ptr, const gdb_byte *op_end,
390 struct insn_info **info)
393 struct cleanup *outer_cleanup, *cleanup;
394 VEC (int) *to_do = NULL;
397 *info = XCNEWVEC (struct insn_info, op_end - op_ptr);
398 outer_cleanup = make_cleanup (xfree, *info);
400 cleanup = make_cleanup (VEC_cleanup (int), &to_do);
402 VEC_safe_push (int, to_do, 0);
403 (*info)[0].depth = initial_depth;
404 (*info)[0].visited = 1;
406 while (!VEC_empty (int, to_do))
408 int ndx = VEC_pop (int, to_do);
410 compute_stack_depth_worker (ndx, need_tempvar, *info, &to_do,
411 byte_order, addr_size,
417 for (i = 0; i < op_end - op_ptr; ++i)
419 if ((*info)[i].depth > stack_depth)
420 stack_depth = (*info)[i].depth;
421 if ((*info)[i].is_tls)
425 do_cleanups (cleanup);
426 discard_cleanups (outer_cleanup);
427 return stack_depth + 1;
432 #define GCC_UINTPTR "__gdb_uintptr"
433 #define GCC_INTPTR "__gdb_intptr"
435 /* Emit code to push a constant. */
438 push (int indent, struct ui_file *stream, ULONGEST l)
440 fprintfi_filtered (indent, stream,
441 "__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
445 /* Emit code to push an arbitrary expression. This works like
448 static void pushf (int indent, struct ui_file *stream, const char *format, ...)
449 ATTRIBUTE_PRINTF (3, 4);
452 pushf (int indent, struct ui_file *stream, const char *format, ...)
456 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos + 1] = ");
457 va_start (args, format);
458 vfprintf_filtered (stream, format, args);
460 fprintf_filtered (stream, ";\n");
462 fprintfi_filtered (indent, stream, "++__gdb_tos;\n");
465 /* Emit code for a unary expression -- one which operates in-place on
466 the top-of-stack. This works like printf. */
468 static void unary (int indent, struct ui_file *stream, const char *format, ...)
469 ATTRIBUTE_PRINTF (3, 4);
472 unary (int indent, struct ui_file *stream, const char *format, ...)
476 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos] = ");
477 va_start (args, format);
478 vfprintf_filtered (stream, format, args);
480 fprintf_filtered (stream, ";\n");
483 /* Emit code for a unary expression -- one which uses the top two
484 stack items, popping the topmost one. This works like printf. */
485 static void binary (int indent, struct ui_file *stream, const char *format, ...)
486 ATTRIBUTE_PRINTF (3, 4);
489 binary (int indent, struct ui_file *stream, const char *format, ...)
493 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 1] = ");
494 va_start (args, format);
495 vfprintf_filtered (stream, format, args);
497 fprintf_filtered (stream, ";\n");
498 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
501 /* Print the name of a label given its "SCOPE", an arbitrary integer
502 used for uniqueness, and its TARGET, the bytecode offset
503 corresponding to the label's point of definition. */
506 print_label (struct ui_file *stream, unsigned int scope, int target)
508 fprintf_filtered (stream, "__label_%u_%s",
509 scope, pulongest (target));
512 /* Emit code that pushes a register's address on the stack.
513 REGISTERS_USED is an out parameter which is updated to note which
514 register was needed by this expression. */
517 pushf_register_address (int indent, struct ui_file *stream,
518 unsigned char *registers_used,
519 struct gdbarch *gdbarch, int regnum)
521 char *regname = compile_register_name_mangled (gdbarch, regnum);
522 struct cleanup *cleanups = make_cleanup (xfree, regname);
524 registers_used[regnum] = 1;
525 pushf (indent, stream,
526 "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
529 do_cleanups (cleanups);
532 /* Emit code that pushes a register's value on the stack.
533 REGISTERS_USED is an out parameter which is updated to note which
534 register was needed by this expression. OFFSET is added to the
535 register's value before it is pushed. */
538 pushf_register (int indent, struct ui_file *stream,
539 unsigned char *registers_used,
540 struct gdbarch *gdbarch, int regnum, uint64_t offset)
542 char *regname = compile_register_name_mangled (gdbarch, regnum);
543 struct cleanup *cleanups = make_cleanup (xfree, regname);
545 registers_used[regnum] = 1;
547 pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
550 pushf (indent, stream,
551 COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s",
552 regname, hex_string (offset));
554 do_cleanups (cleanups);
557 /* Compile a DWARF expression to C code.
559 INDENT is the indentation level to use.
560 STREAM is the stream where the code should be written.
562 TYPE_NAME names the type of the result of the DWARF expression.
563 For locations this is "void *" but for array bounds it will be an
566 RESULT_NAME is the name of a variable in the resulting C code. The
567 result of the expression will be assigned to this variable.
569 SYM is the symbol corresponding to this expression.
570 PC is the location at which the expression is being evaluated.
571 ARCH is the architecture to use.
573 REGISTERS_USED is an out parameter which is updated to note which
574 registers were needed by this expression.
576 ADDR_SIZE is the DWARF address size to use.
578 OPT_PTR and OP_END are the bounds of the DWARF expression.
580 If non-NULL, INITIAL points to an initial value to write to the
581 stack. If NULL, no initial value is written.
583 PER_CU is the per-CU object used for looking up various other
587 do_compile_dwarf_expr_to_c (int indent, struct ui_file *stream,
588 const char *type_name,
589 const char *result_name,
590 struct symbol *sym, CORE_ADDR pc,
591 struct gdbarch *arch,
592 unsigned char *registers_used,
593 unsigned int addr_size,
594 const gdb_byte *op_ptr, const gdb_byte *op_end,
596 struct dwarf2_per_cu_data *per_cu)
598 /* We keep a counter so that labels and other objects we create have
600 static unsigned int scope;
602 enum bfd_endian byte_order = gdbarch_byte_order (arch);
603 const gdb_byte * const base = op_ptr;
604 int need_tempvar = 0;
606 struct cleanup *cleanup;
607 struct insn_info *info;
612 fprintfi_filtered (indent, stream, "__attribute__ ((unused)) %s %s;\n",
613 type_name, result_name);
614 fprintfi_filtered (indent, stream, "{\n");
617 stack_depth = compute_stack_depth (byte_order, addr_size,
618 &need_tempvar, &is_tls,
619 op_ptr, op_end, initial != NULL,
621 cleanup = make_cleanup (xfree, info);
623 /* This is a hack until we can add a feature to glibc to let us
624 properly generate code for TLS. You might think we could emit
625 the address in the ordinary course of translating
626 DW_OP_GNU_push_tls_address, but since the operand appears on the
627 stack, it is relatively hard to find, and the idea of calling
628 target_translate_tls_address with OFFSET==0 and then adding the
629 offset by hand seemed too hackish. */
632 struct frame_info *frame = get_selected_frame (NULL);
636 error (_("Symbol \"%s\" cannot be used because "
637 "there is no selected frame"),
638 SYMBOL_PRINT_NAME (sym));
640 val = read_var_value (sym, NULL, frame);
641 if (VALUE_LVAL (val) != lval_memory)
642 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
643 "as its address has not been found."),
644 SYMBOL_PRINT_NAME (sym));
646 warning (_("Symbol \"%s\" is thread-local and currently can only "
647 "be referenced from the current thread in "
649 SYMBOL_PRINT_NAME (sym));
651 fprintfi_filtered (indent, stream, "%s = %s;\n",
653 core_addr_to_string (value_address (val)));
654 fprintfi_filtered (indent - 2, stream, "}\n");
655 do_cleanups (cleanup);
659 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_stack[%d];\n",
663 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_tmp;\n");
664 fprintfi_filtered (indent, stream, "int __gdb_tos = -1;\n");
667 pushf (indent, stream, "%s", core_addr_to_string (*initial));
669 while (op_ptr < op_end)
671 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
672 uint64_t uoffset, reg;
675 print_spaces (indent - 2, stream);
676 if (info[op_ptr - base].label)
678 print_label (stream, scope, op_ptr - base);
679 fprintf_filtered (stream, ":;");
681 fprintf_filtered (stream, "/* %s */\n", get_DW_OP_name (op));
683 /* This is handy for debugging the generated code:
684 fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
685 (int) info[op_ptr - base].depth - 1);
724 push (indent, stream, op - DW_OP_lit0);
729 /* Some versions of GCC emit DW_OP_addr before
730 DW_OP_GNU_push_tls_address. In this case the value is an
731 index, not an address. We don't support things like
732 branching between the address and the TLS op. */
733 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
734 uoffset += dwarf2_per_cu_text_offset (per_cu);
735 push (indent, stream, uoffset);
739 push (indent, stream,
740 extract_unsigned_integer (op_ptr, 1, byte_order));
744 push (indent, stream,
745 extract_signed_integer (op_ptr, 1, byte_order));
749 push (indent, stream,
750 extract_unsigned_integer (op_ptr, 2, byte_order));
754 push (indent, stream,
755 extract_signed_integer (op_ptr, 2, byte_order));
759 push (indent, stream,
760 extract_unsigned_integer (op_ptr, 4, byte_order));
764 push (indent, stream,
765 extract_signed_integer (op_ptr, 4, byte_order));
769 push (indent, stream,
770 extract_unsigned_integer (op_ptr, 8, byte_order));
774 push (indent, stream,
775 extract_signed_integer (op_ptr, 8, byte_order));
779 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
780 push (indent, stream, uoffset);
783 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
784 push (indent, stream, offset);
819 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
820 pushf_register_address (indent, stream, registers_used, arch,
821 dwarf_reg_to_regnum_or_error
822 (arch, op - DW_OP_reg0));
826 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
827 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
828 pushf_register_address (indent, stream, registers_used, arch,
829 dwarf_reg_to_regnum_or_error (arch, reg));
864 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
865 pushf_register (indent, stream, registers_used, arch,
866 dwarf_reg_to_regnum_or_error (arch,
872 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
873 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
874 pushf_register (indent, stream, registers_used, arch,
875 dwarf_reg_to_regnum_or_error (arch, reg), offset);
880 const gdb_byte *datastart;
882 const struct block *b;
883 struct symbol *framefunc;
886 b = block_for_pc (pc);
889 error (_("No block found for address"));
891 framefunc = block_linkage_function (b);
894 error (_("No function found for block"));
896 func_get_frame_base_dwarf_block (framefunc, pc,
897 &datastart, &datalen);
899 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
901 /* Generate a unique-enough name, in case the frame base
902 is computed multiple times in this expression. */
903 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
904 (long) (op_ptr - base));
906 do_compile_dwarf_expr_to_c (indent, stream,
907 GCC_UINTPTR, fb_name,
909 arch, registers_used, addr_size,
910 datastart, datastart + datalen,
913 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
918 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
922 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
927 pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
932 fprintfi_filtered (indent, stream,
933 "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
934 fprintfi_filtered (indent, stream,
935 "__gdb_stack[__gdb_tos - 1] = "
936 "__gdb_stack[__gdb_tos];\n");
937 fprintfi_filtered (indent, stream, ("__gdb_stack[__gdb_tos] = "
942 pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
946 fprintfi_filtered (indent, stream, ("__gdb_tmp = "
947 "__gdb_stack[__gdb_tos];\n"));
948 fprintfi_filtered (indent, stream,
949 "__gdb_stack[__gdb_tos] = "
950 "__gdb_stack[__gdb_tos - 1];\n");
951 fprintfi_filtered (indent, stream,
952 "__gdb_stack[__gdb_tos - 1] = "
953 "__gdb_stack[__gdb_tos -2];\n");
954 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 2] = "
959 case DW_OP_deref_size:
964 if (op == DW_OP_deref_size)
969 mode = c_get_mode_for_size (size);
971 error (_("Unsupported size %d in %s"),
972 size, get_DW_OP_name (op));
974 /* Cast to a pointer of the desired type, then
976 fprintfi_filtered (indent, stream,
977 "__gdb_stack[__gdb_tos] = "
978 "*((__gdb_int_%s *) "
979 "__gdb_stack[__gdb_tos]);\n",
985 unary (indent, stream,
986 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
987 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
991 unary (indent, stream, "-__gdb_stack[__gdb_tos]");
995 unary (indent, stream, "~__gdb_stack[__gdb_tos]");
998 case DW_OP_plus_uconst:
999 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
1000 unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
1005 binary (indent, stream, ("((" GCC_INTPTR
1006 ") __gdb_stack[__gdb_tos-1]) / (("
1007 GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
1011 binary (indent, stream,
1012 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
1013 "__gdb_stack[__gdb_tos]");
1016 #define BINARY(OP) \
1017 binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1018 " __gdb_stack[__gdb_tos]"); \
1041 #define COMPARE(OP) \
1042 binary (indent, stream, \
1043 "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1045 ") __gdb_stack[__gdb_tos]))"); \
1062 case DW_OP_call_frame_cfa:
1065 CORE_ADDR text_offset;
1067 const gdb_byte *cfa_start, *cfa_end;
1069 if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1071 &text_offset, &cfa_start, &cfa_end))
1074 pushf_register (indent, stream, registers_used, arch, regnum,
1079 /* Another expression. */
1082 /* Generate a unique-enough name, in case the CFA is
1083 computed multiple times in this expression. */
1084 xsnprintf (cfa_name, sizeof (cfa_name),
1085 "__cfa_%ld", (long) (op_ptr - base));
1087 do_compile_dwarf_expr_to_c (indent, stream,
1088 GCC_UINTPTR, cfa_name,
1089 sym, pc, arch, registers_used,
1092 &text_offset, per_cu);
1093 pushf (indent, stream, "%s", cfa_name);
1100 offset = extract_signed_integer (op_ptr, 2, byte_order);
1102 fprintfi_filtered (indent, stream, "goto ");
1103 print_label (stream, scope, op_ptr + offset - base);
1104 fprintf_filtered (stream, ";\n");
1108 offset = extract_signed_integer (op_ptr, 2, byte_order);
1110 fprintfi_filtered (indent, stream,
1111 "if ((( " GCC_INTPTR
1112 ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1113 print_label (stream, scope, op_ptr + offset - base);
1114 fprintf_filtered (stream, ";\n");
1121 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1125 fprintfi_filtered (indent, stream, "%s = __gdb_stack[__gdb_tos];\n",
1127 fprintfi_filtered (indent - 2, stream, "}\n");
1129 do_cleanups (cleanup);
1132 /* See compile.h. */
1135 compile_dwarf_expr_to_c (struct ui_file *stream, const char *result_name,
1136 struct symbol *sym, CORE_ADDR pc,
1137 struct gdbarch *arch, unsigned char *registers_used,
1138 unsigned int addr_size,
1139 const gdb_byte *op_ptr, const gdb_byte *op_end,
1140 struct dwarf2_per_cu_data *per_cu)
1142 do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1143 arch, registers_used, addr_size, op_ptr, op_end,
1147 /* See compile.h. */
1150 compile_dwarf_bounds_to_c (struct ui_file *stream,
1151 const char *result_name,
1152 const struct dynamic_prop *prop,
1153 struct symbol *sym, CORE_ADDR pc,
1154 struct gdbarch *arch, unsigned char *registers_used,
1155 unsigned int addr_size,
1156 const gdb_byte *op_ptr, const gdb_byte *op_end,
1157 struct dwarf2_per_cu_data *per_cu)
1159 do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1160 sym, pc, arch, registers_used,
1161 addr_size, op_ptr, op_end, NULL, per_cu);