1 /* Convert a DWARF location expression to C
3 Copyright (C) 2014-2017 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,
75 std::vector<int> *to_do,
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 to_do->push_back (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 to_do->push_back (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;
394 std::vector<int> to_do;
397 *info = XCNEWVEC (struct insn_info, op_end - op_ptr);
398 outer_cleanup = make_cleanup (xfree, *info);
401 (*info)[0].depth = initial_depth;
402 (*info)[0].visited = 1;
404 while (!to_do.empty ())
406 int ndx = to_do.back ();
409 compute_stack_depth_worker (ndx, need_tempvar, *info, &to_do,
410 byte_order, addr_size,
416 for (i = 0; i < op_end - op_ptr; ++i)
418 if ((*info)[i].depth > stack_depth)
419 stack_depth = (*info)[i].depth;
420 if ((*info)[i].is_tls)
424 discard_cleanups (outer_cleanup);
425 return stack_depth + 1;
430 #define GCC_UINTPTR "__gdb_uintptr"
431 #define GCC_INTPTR "__gdb_intptr"
433 /* Emit code to push a constant. */
436 push (int indent, string_file &stream, ULONGEST l)
438 fprintfi_filtered (indent, &stream,
439 "__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
443 /* Emit code to push an arbitrary expression. This works like
446 static void pushf (int indent, string_file &stream, const char *format, ...)
447 ATTRIBUTE_PRINTF (3, 4);
450 pushf (int indent, string_file &stream, const char *format, ...)
454 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos + 1] = ");
455 va_start (args, format);
456 stream.vprintf (format, args);
460 fprintfi_filtered (indent, &stream, "++__gdb_tos;\n");
463 /* Emit code for a unary expression -- one which operates in-place on
464 the top-of-stack. This works like printf. */
466 static void unary (int indent, string_file &stream, const char *format, ...)
467 ATTRIBUTE_PRINTF (3, 4);
470 unary (int indent, string_file &stream, const char *format, ...)
474 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos] = ");
475 va_start (args, format);
476 stream.vprintf (format, args);
481 /* Emit code for a unary expression -- one which uses the top two
482 stack items, popping the topmost one. This works like printf. */
483 static void binary (int indent, string_file &stream, const char *format, ...)
484 ATTRIBUTE_PRINTF (3, 4);
487 binary (int indent, string_file &stream, const char *format, ...)
491 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos - 1] = ");
492 va_start (args, format);
493 stream.vprintf (format, args);
496 fprintfi_filtered (indent, &stream, "--__gdb_tos;\n");
499 /* Print the name of a label given its "SCOPE", an arbitrary integer
500 used for uniqueness, and its TARGET, the bytecode offset
501 corresponding to the label's point of definition. */
504 print_label (string_file &stream, unsigned int scope, int target)
506 stream.printf ("__label_%u_%s", scope, pulongest (target));
509 /* Emit code that pushes a register's address on the stack.
510 REGISTERS_USED is an out parameter which is updated to note which
511 register was needed by this expression. */
514 pushf_register_address (int indent, string_file &stream,
515 unsigned char *registers_used,
516 struct gdbarch *gdbarch, int regnum)
518 char *regname = compile_register_name_mangled (gdbarch, regnum);
519 struct cleanup *cleanups = make_cleanup (xfree, regname);
521 registers_used[regnum] = 1;
522 pushf (indent, stream,
523 "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
526 do_cleanups (cleanups);
529 /* Emit code that pushes a register's value on the stack.
530 REGISTERS_USED is an out parameter which is updated to note which
531 register was needed by this expression. OFFSET is added to the
532 register's value before it is pushed. */
535 pushf_register (int indent, string_file &stream,
536 unsigned char *registers_used,
537 struct gdbarch *gdbarch, int regnum, uint64_t offset)
539 char *regname = compile_register_name_mangled (gdbarch, regnum);
540 struct cleanup *cleanups = make_cleanup (xfree, regname);
542 registers_used[regnum] = 1;
544 pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
547 pushf (indent, stream,
548 COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s",
549 regname, hex_string (offset));
551 do_cleanups (cleanups);
554 /* Compile a DWARF expression to C code.
556 INDENT is the indentation level to use.
557 STREAM is the stream where the code should be written.
559 TYPE_NAME names the type of the result of the DWARF expression.
560 For locations this is "void *" but for array bounds it will be an
563 RESULT_NAME is the name of a variable in the resulting C code. The
564 result of the expression will be assigned to this variable.
566 SYM is the symbol corresponding to this expression.
567 PC is the location at which the expression is being evaluated.
568 ARCH is the architecture to use.
570 REGISTERS_USED is an out parameter which is updated to note which
571 registers were needed by this expression.
573 ADDR_SIZE is the DWARF address size to use.
575 OPT_PTR and OP_END are the bounds of the DWARF expression.
577 If non-NULL, INITIAL points to an initial value to write to the
578 stack. If NULL, no initial value is written.
580 PER_CU is the per-CU object used for looking up various other
584 do_compile_dwarf_expr_to_c (int indent, string_file &stream,
585 const char *type_name,
586 const char *result_name,
587 struct symbol *sym, CORE_ADDR pc,
588 struct gdbarch *arch,
589 unsigned char *registers_used,
590 unsigned int addr_size,
591 const gdb_byte *op_ptr, const gdb_byte *op_end,
593 struct dwarf2_per_cu_data *per_cu)
595 /* We keep a counter so that labels and other objects we create have
597 static unsigned int scope;
599 enum bfd_endian byte_order = gdbarch_byte_order (arch);
600 const gdb_byte * const base = op_ptr;
601 int need_tempvar = 0;
603 struct cleanup *cleanup;
604 struct insn_info *info;
609 fprintfi_filtered (indent, &stream, "__attribute__ ((unused)) %s %s;\n",
610 type_name, result_name);
611 fprintfi_filtered (indent, &stream, "{\n");
614 stack_depth = compute_stack_depth (byte_order, addr_size,
615 &need_tempvar, &is_tls,
616 op_ptr, op_end, initial != NULL,
618 cleanup = make_cleanup (xfree, info);
620 /* This is a hack until we can add a feature to glibc to let us
621 properly generate code for TLS. You might think we could emit
622 the address in the ordinary course of translating
623 DW_OP_GNU_push_tls_address, but since the operand appears on the
624 stack, it is relatively hard to find, and the idea of calling
625 target_translate_tls_address with OFFSET==0 and then adding the
626 offset by hand seemed too hackish. */
629 struct frame_info *frame = get_selected_frame (NULL);
633 error (_("Symbol \"%s\" cannot be used because "
634 "there is no selected frame"),
635 SYMBOL_PRINT_NAME (sym));
637 val = read_var_value (sym, NULL, frame);
638 if (VALUE_LVAL (val) != lval_memory)
639 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
640 "as its address has not been found."),
641 SYMBOL_PRINT_NAME (sym));
643 warning (_("Symbol \"%s\" is thread-local and currently can only "
644 "be referenced from the current thread in "
646 SYMBOL_PRINT_NAME (sym));
648 fprintfi_filtered (indent, &stream, "%s = %s;\n",
650 core_addr_to_string (value_address (val)));
651 fprintfi_filtered (indent - 2, &stream, "}\n");
652 do_cleanups (cleanup);
656 fprintfi_filtered (indent, &stream, GCC_UINTPTR " __gdb_stack[%d];\n",
660 fprintfi_filtered (indent, &stream, GCC_UINTPTR " __gdb_tmp;\n");
661 fprintfi_filtered (indent, &stream, "int __gdb_tos = -1;\n");
664 pushf (indent, stream, "%s", core_addr_to_string (*initial));
666 while (op_ptr < op_end)
668 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
669 uint64_t uoffset, reg;
672 print_spaces (indent - 2, &stream);
673 if (info[op_ptr - base].label)
675 print_label (stream, scope, op_ptr - base);
678 stream.printf ("/* %s */\n", get_DW_OP_name (op));
680 /* This is handy for debugging the generated code:
681 fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
682 (int) info[op_ptr - base].depth - 1);
721 push (indent, stream, op - DW_OP_lit0);
726 /* Some versions of GCC emit DW_OP_addr before
727 DW_OP_GNU_push_tls_address. In this case the value is an
728 index, not an address. We don't support things like
729 branching between the address and the TLS op. */
730 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
731 uoffset += dwarf2_per_cu_text_offset (per_cu);
732 push (indent, stream, uoffset);
736 push (indent, stream,
737 extract_unsigned_integer (op_ptr, 1, byte_order));
741 push (indent, stream,
742 extract_signed_integer (op_ptr, 1, byte_order));
746 push (indent, stream,
747 extract_unsigned_integer (op_ptr, 2, byte_order));
751 push (indent, stream,
752 extract_signed_integer (op_ptr, 2, byte_order));
756 push (indent, stream,
757 extract_unsigned_integer (op_ptr, 4, byte_order));
761 push (indent, stream,
762 extract_signed_integer (op_ptr, 4, byte_order));
766 push (indent, stream,
767 extract_unsigned_integer (op_ptr, 8, byte_order));
771 push (indent, stream,
772 extract_signed_integer (op_ptr, 8, byte_order));
776 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
777 push (indent, stream, uoffset);
780 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
781 push (indent, stream, offset);
816 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
817 pushf_register_address (indent, stream, registers_used, arch,
818 dwarf_reg_to_regnum_or_error
819 (arch, op - DW_OP_reg0));
823 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
824 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
825 pushf_register_address (indent, stream, registers_used, arch,
826 dwarf_reg_to_regnum_or_error (arch, reg));
861 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
862 pushf_register (indent, stream, registers_used, arch,
863 dwarf_reg_to_regnum_or_error (arch,
869 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
870 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
871 pushf_register (indent, stream, registers_used, arch,
872 dwarf_reg_to_regnum_or_error (arch, reg), offset);
877 const gdb_byte *datastart;
879 const struct block *b;
880 struct symbol *framefunc;
883 b = block_for_pc (pc);
886 error (_("No block found for address"));
888 framefunc = block_linkage_function (b);
891 error (_("No function found for block"));
893 func_get_frame_base_dwarf_block (framefunc, pc,
894 &datastart, &datalen);
896 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
898 /* Generate a unique-enough name, in case the frame base
899 is computed multiple times in this expression. */
900 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
901 (long) (op_ptr - base));
903 do_compile_dwarf_expr_to_c (indent, stream,
904 GCC_UINTPTR, fb_name,
906 arch, registers_used, addr_size,
907 datastart, datastart + datalen,
910 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
915 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
919 fprintfi_filtered (indent, &stream, "--__gdb_tos;\n");
924 pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
929 fprintfi_filtered (indent, &stream,
930 "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
931 fprintfi_filtered (indent, &stream,
932 "__gdb_stack[__gdb_tos - 1] = "
933 "__gdb_stack[__gdb_tos];\n");
934 fprintfi_filtered (indent, &stream, ("__gdb_stack[__gdb_tos] = "
939 pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
943 fprintfi_filtered (indent, &stream, ("__gdb_tmp = "
944 "__gdb_stack[__gdb_tos];\n"));
945 fprintfi_filtered (indent, &stream,
946 "__gdb_stack[__gdb_tos] = "
947 "__gdb_stack[__gdb_tos - 1];\n");
948 fprintfi_filtered (indent, &stream,
949 "__gdb_stack[__gdb_tos - 1] = "
950 "__gdb_stack[__gdb_tos -2];\n");
951 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos - 2] = "
956 case DW_OP_deref_size:
961 if (op == DW_OP_deref_size)
966 mode = c_get_mode_for_size (size);
968 error (_("Unsupported size %d in %s"),
969 size, get_DW_OP_name (op));
971 /* Cast to a pointer of the desired type, then
973 fprintfi_filtered (indent, &stream,
974 "__gdb_stack[__gdb_tos] = "
975 "*((__gdb_int_%s *) "
976 "__gdb_stack[__gdb_tos]);\n",
982 unary (indent, stream,
983 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
984 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
988 unary (indent, stream, "-__gdb_stack[__gdb_tos]");
992 unary (indent, stream, "~__gdb_stack[__gdb_tos]");
995 case DW_OP_plus_uconst:
996 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
997 unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
1002 binary (indent, stream, ("((" GCC_INTPTR
1003 ") __gdb_stack[__gdb_tos-1]) / (("
1004 GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
1008 binary (indent, stream,
1009 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
1010 "__gdb_stack[__gdb_tos]");
1013 #define BINARY(OP) \
1014 binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1015 " __gdb_stack[__gdb_tos]"); \
1038 #define COMPARE(OP) \
1039 binary (indent, stream, \
1040 "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1042 ") __gdb_stack[__gdb_tos]))"); \
1059 case DW_OP_call_frame_cfa:
1062 CORE_ADDR text_offset;
1064 const gdb_byte *cfa_start, *cfa_end;
1066 if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1068 &text_offset, &cfa_start, &cfa_end))
1071 pushf_register (indent, stream, registers_used, arch, regnum,
1076 /* Another expression. */
1079 /* Generate a unique-enough name, in case the CFA is
1080 computed multiple times in this expression. */
1081 xsnprintf (cfa_name, sizeof (cfa_name),
1082 "__cfa_%ld", (long) (op_ptr - base));
1084 do_compile_dwarf_expr_to_c (indent, stream,
1085 GCC_UINTPTR, cfa_name,
1086 sym, pc, arch, registers_used,
1089 &text_offset, per_cu);
1090 pushf (indent, stream, "%s", cfa_name);
1097 offset = extract_signed_integer (op_ptr, 2, byte_order);
1099 fprintfi_filtered (indent, &stream, "goto ");
1100 print_label (stream, scope, op_ptr + offset - base);
1101 stream.puts (";\n");
1105 offset = extract_signed_integer (op_ptr, 2, byte_order);
1107 fprintfi_filtered (indent, &stream,
1108 "if ((( " GCC_INTPTR
1109 ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1110 print_label (stream, scope, op_ptr + offset - base);
1111 stream.puts (";\n");
1118 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1122 fprintfi_filtered (indent, &stream, "%s = __gdb_stack[__gdb_tos];\n",
1124 fprintfi_filtered (indent - 2, &stream, "}\n");
1126 do_cleanups (cleanup);
1129 /* See compile.h. */
1132 compile_dwarf_expr_to_c (string_file &stream, const char *result_name,
1133 struct symbol *sym, CORE_ADDR pc,
1134 struct gdbarch *arch, unsigned char *registers_used,
1135 unsigned int addr_size,
1136 const gdb_byte *op_ptr, const gdb_byte *op_end,
1137 struct dwarf2_per_cu_data *per_cu)
1139 do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1140 arch, registers_used, addr_size, op_ptr, op_end,
1144 /* See compile.h. */
1147 compile_dwarf_bounds_to_c (string_file &stream,
1148 const char *result_name,
1149 const struct dynamic_prop *prop,
1150 struct symbol *sym, CORE_ADDR pc,
1151 struct gdbarch *arch, unsigned char *registers_used,
1152 unsigned int addr_size,
1153 const gdb_byte *op_ptr, const gdb_byte *op_end,
1154 struct dwarf2_per_cu_data *per_cu)
1156 do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1157 sym, pc, arch, registers_used,
1158 addr_size, op_ptr, op_end, NULL, per_cu);