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);
725 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
727 /* Some versions of GCC emit DW_OP_addr before
728 DW_OP_GNU_push_tls_address. In this case the value is an
729 index, not an address. We don't support things like
730 branching between the address and the TLS op. */
731 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
732 uoffset += dwarf2_per_cu_text_offset (per_cu);
733 push (indent, stream, uoffset);
737 push (indent, stream,
738 extract_unsigned_integer (op_ptr, 1, byte_order));
742 push (indent, stream,
743 extract_signed_integer (op_ptr, 1, byte_order));
747 push (indent, stream,
748 extract_unsigned_integer (op_ptr, 2, byte_order));
752 push (indent, stream,
753 extract_signed_integer (op_ptr, 2, byte_order));
757 push (indent, stream,
758 extract_unsigned_integer (op_ptr, 4, byte_order));
762 push (indent, stream,
763 extract_signed_integer (op_ptr, 4, byte_order));
767 push (indent, stream,
768 extract_unsigned_integer (op_ptr, 8, byte_order));
772 push (indent, stream,
773 extract_signed_integer (op_ptr, 8, byte_order));
777 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
778 push (indent, stream, uoffset);
781 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
782 push (indent, stream, offset);
817 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
818 pushf_register_address (indent, stream, registers_used, arch,
819 dwarf_reg_to_regnum_or_error
820 (arch, op - DW_OP_reg0));
824 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
825 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
826 pushf_register_address (indent, stream, registers_used, arch,
827 dwarf_reg_to_regnum_or_error (arch, reg));
862 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
863 pushf_register (indent, stream, registers_used, arch,
864 dwarf_reg_to_regnum_or_error (arch,
870 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
871 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
872 pushf_register (indent, stream, registers_used, arch,
873 dwarf_reg_to_regnum_or_error (arch, reg), offset);
878 const gdb_byte *datastart;
880 const struct block *b;
881 struct symbol *framefunc;
884 b = block_for_pc (pc);
887 error (_("No block found for address"));
889 framefunc = block_linkage_function (b);
892 error (_("No function found for block"));
894 func_get_frame_base_dwarf_block (framefunc, pc,
895 &datastart, &datalen);
897 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
899 /* Generate a unique-enough name, in case the frame base
900 is computed multiple times in this expression. */
901 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
902 (long) (op_ptr - base));
904 do_compile_dwarf_expr_to_c (indent, stream,
905 GCC_UINTPTR, fb_name,
907 arch, registers_used, addr_size,
908 datastart, datastart + datalen,
911 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
916 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
920 fprintfi_filtered (indent, &stream, "--__gdb_tos;\n");
925 pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
930 fprintfi_filtered (indent, &stream,
931 "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
932 fprintfi_filtered (indent, &stream,
933 "__gdb_stack[__gdb_tos - 1] = "
934 "__gdb_stack[__gdb_tos];\n");
935 fprintfi_filtered (indent, &stream, ("__gdb_stack[__gdb_tos] = "
940 pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
944 fprintfi_filtered (indent, &stream, ("__gdb_tmp = "
945 "__gdb_stack[__gdb_tos];\n"));
946 fprintfi_filtered (indent, &stream,
947 "__gdb_stack[__gdb_tos] = "
948 "__gdb_stack[__gdb_tos - 1];\n");
949 fprintfi_filtered (indent, &stream,
950 "__gdb_stack[__gdb_tos - 1] = "
951 "__gdb_stack[__gdb_tos -2];\n");
952 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos - 2] = "
957 case DW_OP_deref_size:
962 if (op == DW_OP_deref_size)
967 mode = c_get_mode_for_size (size);
969 error (_("Unsupported size %d in %s"),
970 size, get_DW_OP_name (op));
972 /* Cast to a pointer of the desired type, then
974 fprintfi_filtered (indent, &stream,
975 "__gdb_stack[__gdb_tos] = "
976 "*((__gdb_int_%s *) "
977 "__gdb_stack[__gdb_tos]);\n",
983 unary (indent, stream,
984 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
985 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
989 unary (indent, stream, "-__gdb_stack[__gdb_tos]");
993 unary (indent, stream, "~__gdb_stack[__gdb_tos]");
996 case DW_OP_plus_uconst:
997 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
998 unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
1003 binary (indent, stream, ("((" GCC_INTPTR
1004 ") __gdb_stack[__gdb_tos-1]) / (("
1005 GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
1009 binary (indent, stream,
1010 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
1011 "__gdb_stack[__gdb_tos]");
1014 #define BINARY(OP) \
1015 binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1016 " __gdb_stack[__gdb_tos]"); \
1039 #define COMPARE(OP) \
1040 binary (indent, stream, \
1041 "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1043 ") __gdb_stack[__gdb_tos]))"); \
1060 case DW_OP_call_frame_cfa:
1063 CORE_ADDR text_offset;
1065 const gdb_byte *cfa_start, *cfa_end;
1067 if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1069 &text_offset, &cfa_start, &cfa_end))
1072 pushf_register (indent, stream, registers_used, arch, regnum,
1077 /* Another expression. */
1080 /* Generate a unique-enough name, in case the CFA is
1081 computed multiple times in this expression. */
1082 xsnprintf (cfa_name, sizeof (cfa_name),
1083 "__cfa_%ld", (long) (op_ptr - base));
1085 do_compile_dwarf_expr_to_c (indent, stream,
1086 GCC_UINTPTR, cfa_name,
1087 sym, pc, arch, registers_used,
1090 &text_offset, per_cu);
1091 pushf (indent, stream, "%s", cfa_name);
1098 offset = extract_signed_integer (op_ptr, 2, byte_order);
1100 fprintfi_filtered (indent, &stream, "goto ");
1101 print_label (stream, scope, op_ptr + offset - base);
1102 stream.puts (";\n");
1106 offset = extract_signed_integer (op_ptr, 2, byte_order);
1108 fprintfi_filtered (indent, &stream,
1109 "if ((( " GCC_INTPTR
1110 ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1111 print_label (stream, scope, op_ptr + offset - base);
1112 stream.puts (";\n");
1119 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1123 fprintfi_filtered (indent, &stream, "%s = __gdb_stack[__gdb_tos];\n",
1125 fprintfi_filtered (indent - 2, &stream, "}\n");
1127 do_cleanups (cleanup);
1130 /* See compile.h. */
1133 compile_dwarf_expr_to_c (string_file &stream, const char *result_name,
1134 struct symbol *sym, CORE_ADDR pc,
1135 struct gdbarch *arch, unsigned char *registers_used,
1136 unsigned int addr_size,
1137 const gdb_byte *op_ptr, const gdb_byte *op_end,
1138 struct dwarf2_per_cu_data *per_cu)
1140 do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1141 arch, registers_used, addr_size, op_ptr, op_end,
1145 /* See compile.h. */
1148 compile_dwarf_bounds_to_c (string_file &stream,
1149 const char *result_name,
1150 const struct dynamic_prop *prop,
1151 struct symbol *sym, CORE_ADDR pc,
1152 struct gdbarch *arch, unsigned char *registers_used,
1153 unsigned int addr_size,
1154 const gdb_byte *op_ptr, const gdb_byte *op_end,
1155 struct dwarf2_per_cu_data *per_cu)
1157 do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1158 sym, pc, arch, registers_used,
1159 addr_size, op_ptr, op_end, NULL, per_cu);