1 /* Convert a DWARF location expression to C
3 Copyright (C) 2014-2018 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 a vector 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 std::vector<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 std::vector<struct insn_info> *info)
392 std::vector<int> to_do;
395 info->resize (op_end - op_ptr);
398 (*info)[0].depth = initial_depth;
399 (*info)[0].visited = 1;
401 while (!to_do.empty ())
403 int ndx = to_do.back ();
406 compute_stack_depth_worker (ndx, need_tempvar, info, &to_do,
407 byte_order, addr_size,
413 for (i = 0; i < op_end - op_ptr; ++i)
415 if ((*info)[i].depth > stack_depth)
416 stack_depth = (*info)[i].depth;
417 if ((*info)[i].is_tls)
421 return stack_depth + 1;
426 #define GCC_UINTPTR "__gdb_uintptr"
427 #define GCC_INTPTR "__gdb_intptr"
429 /* Emit code to push a constant. */
432 push (int indent, string_file &stream, ULONGEST l)
434 fprintfi_filtered (indent, &stream,
435 "__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
439 /* Emit code to push an arbitrary expression. This works like
442 static void pushf (int indent, string_file &stream, const char *format, ...)
443 ATTRIBUTE_PRINTF (3, 4);
446 pushf (int indent, string_file &stream, const char *format, ...)
450 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos + 1] = ");
451 va_start (args, format);
452 stream.vprintf (format, args);
456 fprintfi_filtered (indent, &stream, "++__gdb_tos;\n");
459 /* Emit code for a unary expression -- one which operates in-place on
460 the top-of-stack. This works like printf. */
462 static void unary (int indent, string_file &stream, const char *format, ...)
463 ATTRIBUTE_PRINTF (3, 4);
466 unary (int indent, string_file &stream, const char *format, ...)
470 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos] = ");
471 va_start (args, format);
472 stream.vprintf (format, args);
477 /* Emit code for a unary expression -- one which uses the top two
478 stack items, popping the topmost one. This works like printf. */
479 static void binary (int indent, string_file &stream, const char *format, ...)
480 ATTRIBUTE_PRINTF (3, 4);
483 binary (int indent, string_file &stream, const char *format, ...)
487 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos - 1] = ");
488 va_start (args, format);
489 stream.vprintf (format, args);
492 fprintfi_filtered (indent, &stream, "--__gdb_tos;\n");
495 /* Print the name of a label given its "SCOPE", an arbitrary integer
496 used for uniqueness, and its TARGET, the bytecode offset
497 corresponding to the label's point of definition. */
500 print_label (string_file &stream, unsigned int scope, int target)
502 stream.printf ("__label_%u_%s", scope, pulongest (target));
505 /* Emit code that pushes a register's address on the stack.
506 REGISTERS_USED is an out parameter which is updated to note which
507 register was needed by this expression. */
510 pushf_register_address (int indent, string_file &stream,
511 unsigned char *registers_used,
512 struct gdbarch *gdbarch, int regnum)
514 std::string regname = compile_register_name_mangled (gdbarch, regnum);
516 registers_used[regnum] = 1;
517 pushf (indent, stream,
518 "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
522 /* Emit code that pushes a register's value on the stack.
523 REGISTERS_USED is an out parameter which is updated to note which
524 register was needed by this expression. OFFSET is added to the
525 register's value before it is pushed. */
528 pushf_register (int indent, string_file &stream,
529 unsigned char *registers_used,
530 struct gdbarch *gdbarch, int regnum, uint64_t offset)
532 std::string regname = compile_register_name_mangled (gdbarch, regnum);
534 registers_used[regnum] = 1;
536 pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
539 pushf (indent, stream,
540 COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s",
541 regname.c_str (), hex_string (offset));
544 /* Compile a DWARF expression to C code.
546 INDENT is the indentation level to use.
547 STREAM is the stream where the code should be written.
549 TYPE_NAME names the type of the result of the DWARF expression.
550 For locations this is "void *" but for array bounds it will be an
553 RESULT_NAME is the name of a variable in the resulting C code. The
554 result of the expression will be assigned to this variable.
556 SYM is the symbol corresponding to this expression.
557 PC is the location at which the expression is being evaluated.
558 ARCH is the architecture to use.
560 REGISTERS_USED is an out parameter which is updated to note which
561 registers were needed by this expression.
563 ADDR_SIZE is the DWARF address size to use.
565 OPT_PTR and OP_END are the bounds of the DWARF expression.
567 If non-NULL, INITIAL points to an initial value to write to the
568 stack. If NULL, no initial value is written.
570 PER_CU is the per-CU object used for looking up various other
574 do_compile_dwarf_expr_to_c (int indent, string_file &stream,
575 const char *type_name,
576 const char *result_name,
577 struct symbol *sym, CORE_ADDR pc,
578 struct gdbarch *arch,
579 unsigned char *registers_used,
580 unsigned int addr_size,
581 const gdb_byte *op_ptr, const gdb_byte *op_end,
583 struct dwarf2_per_cu_data *per_cu)
585 /* We keep a counter so that labels and other objects we create have
587 static unsigned int scope;
589 enum bfd_endian byte_order = gdbarch_byte_order (arch);
590 const gdb_byte * const base = op_ptr;
591 int need_tempvar = 0;
593 std::vector<struct insn_info> info;
598 fprintfi_filtered (indent, &stream, "__attribute__ ((unused)) %s %s;\n",
599 type_name, result_name);
600 fprintfi_filtered (indent, &stream, "{\n");
603 stack_depth = compute_stack_depth (byte_order, addr_size,
604 &need_tempvar, &is_tls,
605 op_ptr, op_end, initial != NULL,
608 /* This is a hack until we can add a feature to glibc to let us
609 properly generate code for TLS. You might think we could emit
610 the address in the ordinary course of translating
611 DW_OP_GNU_push_tls_address, but since the operand appears on the
612 stack, it is relatively hard to find, and the idea of calling
613 target_translate_tls_address with OFFSET==0 and then adding the
614 offset by hand seemed too hackish. */
617 struct frame_info *frame = get_selected_frame (NULL);
621 error (_("Symbol \"%s\" cannot be used because "
622 "there is no selected frame"),
623 SYMBOL_PRINT_NAME (sym));
625 val = read_var_value (sym, NULL, frame);
626 if (VALUE_LVAL (val) != lval_memory)
627 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
628 "as its address has not been found."),
629 SYMBOL_PRINT_NAME (sym));
631 warning (_("Symbol \"%s\" is thread-local and currently can only "
632 "be referenced from the current thread in "
634 SYMBOL_PRINT_NAME (sym));
636 fprintfi_filtered (indent, &stream, "%s = %s;\n",
638 core_addr_to_string (value_address (val)));
639 fprintfi_filtered (indent - 2, &stream, "}\n");
643 fprintfi_filtered (indent, &stream, GCC_UINTPTR " __gdb_stack[%d];\n",
647 fprintfi_filtered (indent, &stream, GCC_UINTPTR " __gdb_tmp;\n");
648 fprintfi_filtered (indent, &stream, "int __gdb_tos = -1;\n");
651 pushf (indent, stream, "%s", core_addr_to_string (*initial));
653 while (op_ptr < op_end)
655 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
656 uint64_t uoffset, reg;
659 print_spaces (indent - 2, &stream);
660 if (info[op_ptr - base].label)
662 print_label (stream, scope, op_ptr - base);
665 stream.printf ("/* %s */\n", get_DW_OP_name (op));
667 /* This is handy for debugging the generated code:
668 fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
669 (int) info[op_ptr - base].depth - 1);
708 push (indent, stream, op - DW_OP_lit0);
712 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
714 /* Some versions of GCC emit DW_OP_addr before
715 DW_OP_GNU_push_tls_address. In this case the value is an
716 index, not an address. We don't support things like
717 branching between the address and the TLS op. */
718 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
719 uoffset += dwarf2_per_cu_text_offset (per_cu);
720 push (indent, stream, uoffset);
724 push (indent, stream,
725 extract_unsigned_integer (op_ptr, 1, byte_order));
729 push (indent, stream,
730 extract_signed_integer (op_ptr, 1, byte_order));
734 push (indent, stream,
735 extract_unsigned_integer (op_ptr, 2, byte_order));
739 push (indent, stream,
740 extract_signed_integer (op_ptr, 2, byte_order));
744 push (indent, stream,
745 extract_unsigned_integer (op_ptr, 4, byte_order));
749 push (indent, stream,
750 extract_signed_integer (op_ptr, 4, byte_order));
754 push (indent, stream,
755 extract_unsigned_integer (op_ptr, 8, byte_order));
759 push (indent, stream,
760 extract_signed_integer (op_ptr, 8, byte_order));
764 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
765 push (indent, stream, uoffset);
768 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
769 push (indent, stream, offset);
804 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
805 pushf_register_address (indent, stream, registers_used, arch,
806 dwarf_reg_to_regnum_or_error
807 (arch, op - DW_OP_reg0));
811 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
812 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
813 pushf_register_address (indent, stream, registers_used, arch,
814 dwarf_reg_to_regnum_or_error (arch, reg));
849 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
850 pushf_register (indent, stream, registers_used, arch,
851 dwarf_reg_to_regnum_or_error (arch,
857 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
858 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
859 pushf_register (indent, stream, registers_used, arch,
860 dwarf_reg_to_regnum_or_error (arch, reg), offset);
865 const gdb_byte *datastart;
867 const struct block *b;
868 struct symbol *framefunc;
871 b = block_for_pc (pc);
874 error (_("No block found for address"));
876 framefunc = block_linkage_function (b);
879 error (_("No function found for block"));
881 func_get_frame_base_dwarf_block (framefunc, pc,
882 &datastart, &datalen);
884 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
886 /* Generate a unique-enough name, in case the frame base
887 is computed multiple times in this expression. */
888 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
889 (long) (op_ptr - base));
891 do_compile_dwarf_expr_to_c (indent, stream,
892 GCC_UINTPTR, fb_name,
894 arch, registers_used, addr_size,
895 datastart, datastart + datalen,
898 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
903 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
907 fprintfi_filtered (indent, &stream, "--__gdb_tos;\n");
912 pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
917 fprintfi_filtered (indent, &stream,
918 "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
919 fprintfi_filtered (indent, &stream,
920 "__gdb_stack[__gdb_tos - 1] = "
921 "__gdb_stack[__gdb_tos];\n");
922 fprintfi_filtered (indent, &stream, ("__gdb_stack[__gdb_tos] = "
927 pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
931 fprintfi_filtered (indent, &stream, ("__gdb_tmp = "
932 "__gdb_stack[__gdb_tos];\n"));
933 fprintfi_filtered (indent, &stream,
934 "__gdb_stack[__gdb_tos] = "
935 "__gdb_stack[__gdb_tos - 1];\n");
936 fprintfi_filtered (indent, &stream,
937 "__gdb_stack[__gdb_tos - 1] = "
938 "__gdb_stack[__gdb_tos -2];\n");
939 fprintfi_filtered (indent, &stream, "__gdb_stack[__gdb_tos - 2] = "
944 case DW_OP_deref_size:
949 if (op == DW_OP_deref_size)
954 mode = c_get_mode_for_size (size);
956 error (_("Unsupported size %d in %s"),
957 size, get_DW_OP_name (op));
959 /* Cast to a pointer of the desired type, then
961 fprintfi_filtered (indent, &stream,
962 "__gdb_stack[__gdb_tos] = "
963 "*((__gdb_int_%s *) "
964 "__gdb_stack[__gdb_tos]);\n",
970 unary (indent, stream,
971 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
972 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
976 unary (indent, stream, "-__gdb_stack[__gdb_tos]");
980 unary (indent, stream, "~__gdb_stack[__gdb_tos]");
983 case DW_OP_plus_uconst:
984 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
985 unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
990 binary (indent, stream, ("((" GCC_INTPTR
991 ") __gdb_stack[__gdb_tos-1]) / (("
992 GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
996 binary (indent, stream,
997 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
998 "__gdb_stack[__gdb_tos]");
1001 #define BINARY(OP) \
1002 binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1003 " __gdb_stack[__gdb_tos]"); \
1026 #define COMPARE(OP) \
1027 binary (indent, stream, \
1028 "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1030 ") __gdb_stack[__gdb_tos]))"); \
1047 case DW_OP_call_frame_cfa:
1050 CORE_ADDR text_offset;
1052 const gdb_byte *cfa_start, *cfa_end;
1054 if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1056 &text_offset, &cfa_start, &cfa_end))
1059 pushf_register (indent, stream, registers_used, arch, regnum,
1064 /* Another expression. */
1067 /* Generate a unique-enough name, in case the CFA is
1068 computed multiple times in this expression. */
1069 xsnprintf (cfa_name, sizeof (cfa_name),
1070 "__cfa_%ld", (long) (op_ptr - base));
1072 do_compile_dwarf_expr_to_c (indent, stream,
1073 GCC_UINTPTR, cfa_name,
1074 sym, pc, arch, registers_used,
1077 &text_offset, per_cu);
1078 pushf (indent, stream, "%s", cfa_name);
1085 offset = extract_signed_integer (op_ptr, 2, byte_order);
1087 fprintfi_filtered (indent, &stream, "goto ");
1088 print_label (stream, scope, op_ptr + offset - base);
1089 stream.puts (";\n");
1093 offset = extract_signed_integer (op_ptr, 2, byte_order);
1095 fprintfi_filtered (indent, &stream,
1096 "if ((( " GCC_INTPTR
1097 ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1098 print_label (stream, scope, op_ptr + offset - base);
1099 stream.puts (";\n");
1106 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1110 fprintfi_filtered (indent, &stream, "%s = __gdb_stack[__gdb_tos];\n",
1112 fprintfi_filtered (indent - 2, &stream, "}\n");
1115 /* See compile.h. */
1118 compile_dwarf_expr_to_c (string_file &stream, const char *result_name,
1119 struct symbol *sym, CORE_ADDR pc,
1120 struct gdbarch *arch, unsigned char *registers_used,
1121 unsigned int addr_size,
1122 const gdb_byte *op_ptr, const gdb_byte *op_end,
1123 struct dwarf2_per_cu_data *per_cu)
1125 do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1126 arch, registers_used, addr_size, op_ptr, op_end,
1130 /* See compile.h. */
1133 compile_dwarf_bounds_to_c (string_file &stream,
1134 const char *result_name,
1135 const struct dynamic_prop *prop,
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, "unsigned long ", result_name,
1143 sym, pc, arch, registers_used,
1144 addr_size, op_ptr, op_end, NULL, per_cu);