1 /* Convert a DWARF location expression to C
3 Copyright (C) 2014-2019 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"
27 #include "compile-c.h"
30 #include "dwarf2-frame.h"
36 /* Information about a given instruction. */
40 /* Stack depth at entry. */
44 /* Whether this instruction has been visited. */
46 unsigned int visited : 1;
48 /* Whether this instruction needs a label. */
50 unsigned int label : 1;
52 /* Whether this instruction is DW_OP_GNU_push_tls_address or
53 DW_OP_form_tls_address. This is a hack until we can add a
54 feature to glibc to let us properly generate code for TLS. */
56 unsigned int is_tls : 1;
59 /* A helper function for compute_stack_depth that does the work. This
60 examines the DWARF expression starting from START and computes
63 NEED_TEMPVAR is an out parameter which is set if this expression
64 needs a special temporary variable to be emitted (see the code
66 INFO is a vector of insn_info objects, indexed by offset from the
67 start of the DWARF expression.
68 TO_DO is a list of bytecodes which must be examined; it may be
69 added to by this function.
70 BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
71 OP_PTR and OP_END are the bounds of the DWARF expression. */
74 compute_stack_depth_worker (int start, int *need_tempvar,
75 std::vector<struct insn_info> *info,
76 std::vector<int> *to_do,
77 enum bfd_endian byte_order, unsigned int addr_size,
78 const gdb_byte *op_ptr, const gdb_byte *op_end)
80 const gdb_byte * const base = op_ptr;
84 gdb_assert ((*info)[start].visited);
85 stack_depth = (*info)[start].depth;
87 while (op_ptr < op_end)
89 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
92 int ndx = op_ptr - base;
94 #define SET_CHECK_DEPTH(WHERE) \
95 if ((*info)[WHERE].visited) \
97 if ((*info)[WHERE].depth != stack_depth) \
98 error (_("inconsistent stack depths")); \
102 /* Stack depth not set, so set it. */ \
103 (*info)[WHERE].visited = 1; \
104 (*info)[WHERE].depth = stack_depth; \
107 SET_CHECK_DEPTH (ndx);
175 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
215 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
251 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
256 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
257 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
262 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
294 case DW_OP_deref_size:
298 case DW_OP_plus_uconst:
299 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
322 case DW_OP_call_frame_cfa:
326 case DW_OP_GNU_push_tls_address:
327 case DW_OP_form_tls_address:
328 (*info)[ndx].is_tls = 1;
332 offset = extract_signed_integer (op_ptr, 2, byte_order);
334 offset = op_ptr + offset - base;
335 /* If the destination has not been seen yet, add it to the
337 if (!(*info)[offset].visited)
338 to_do->push_back (offset);
339 SET_CHECK_DEPTH (offset);
340 (*info)[offset].label = 1;
341 /* We're done with this line of code. */
345 offset = extract_signed_integer (op_ptr, 2, byte_order);
347 offset = op_ptr + offset - base;
349 /* If the destination has not been seen yet, add it to the
351 if (!(*info)[offset].visited)
352 to_do->push_back (offset);
353 SET_CHECK_DEPTH (offset);
354 (*info)[offset].label = 1;
361 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
365 gdb_assert (op_ptr == op_end);
367 #undef SET_CHECK_DEPTH
370 /* Compute the maximum needed stack depth of a DWARF expression, and
371 some other information as well.
373 BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
374 NEED_TEMPVAR is an out parameter which is set if this expression
375 needs a special temporary variable to be emitted (see the code
377 IS_TLS is an out parameter which is set if this expression refers
379 OP_PTR and OP_END are the bounds of the DWARF expression.
380 INITIAL_DEPTH is the initial depth of the DWARF expression stack.
381 INFO is an array of insn_info objects, indexed by offset from the
382 start of the DWARF expression.
384 This returns the maximum stack depth. */
387 compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
388 int *need_tempvar, int *is_tls,
389 const gdb_byte *op_ptr, const gdb_byte *op_end,
391 std::vector<struct insn_info> *info)
393 std::vector<int> to_do;
396 info->resize (op_end - op_ptr);
399 (*info)[0].depth = initial_depth;
400 (*info)[0].visited = 1;
402 while (!to_do.empty ())
404 int ndx = to_do.back ();
407 compute_stack_depth_worker (ndx, need_tempvar, info, &to_do,
408 byte_order, addr_size,
414 for (i = 0; i < op_end - op_ptr; ++i)
416 if ((*info)[i].depth > stack_depth)
417 stack_depth = (*info)[i].depth;
418 if ((*info)[i].is_tls)
422 return stack_depth + 1;
427 #define GCC_UINTPTR "__gdb_uintptr"
428 #define GCC_INTPTR "__gdb_intptr"
430 /* Emit code to push a constant. */
433 push (int indent, string_file *stream, ULONGEST l)
435 fprintfi_filtered (indent, stream,
436 "__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
440 /* Emit code to push an arbitrary expression. This works like
443 static void pushf (int indent, string_file *stream, const char *format, ...)
444 ATTRIBUTE_PRINTF (3, 4);
447 pushf (int indent, string_file *stream, const char *format, ...)
451 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos + 1] = ");
452 va_start (args, format);
453 stream->vprintf (format, args);
455 stream->puts (";\n");
457 fprintfi_filtered (indent, stream, "++__gdb_tos;\n");
460 /* Emit code for a unary expression -- one which operates in-place on
461 the top-of-stack. This works like printf. */
463 static void unary (int indent, string_file *stream, const char *format, ...)
464 ATTRIBUTE_PRINTF (3, 4);
467 unary (int indent, string_file *stream, const char *format, ...)
471 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos] = ");
472 va_start (args, format);
473 stream->vprintf (format, args);
475 stream->puts (";\n");
478 /* Emit code for a unary expression -- one which uses the top two
479 stack items, popping the topmost one. This works like printf. */
480 static void binary (int indent, string_file *stream, const char *format, ...)
481 ATTRIBUTE_PRINTF (3, 4);
484 binary (int indent, string_file *stream, const char *format, ...)
488 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 1] = ");
489 va_start (args, format);
490 stream->vprintf (format, args);
492 stream->puts (";\n");
493 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
496 /* Print the name of a label given its "SCOPE", an arbitrary integer
497 used for uniqueness, and its TARGET, the bytecode offset
498 corresponding to the label's point of definition. */
501 print_label (string_file *stream, unsigned int scope, int target)
503 stream->printf ("__label_%u_%s", scope, pulongest (target));
506 /* Emit code that pushes a register's address on the stack.
507 REGISTERS_USED is an out parameter which is updated to note which
508 register was needed by this expression. */
511 pushf_register_address (int indent, string_file *stream,
512 unsigned char *registers_used,
513 struct gdbarch *gdbarch, int regnum)
515 std::string regname = compile_register_name_mangled (gdbarch, regnum);
517 registers_used[regnum] = 1;
518 pushf (indent, stream,
519 "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
523 /* Emit code that pushes a register's value on the stack.
524 REGISTERS_USED is an out parameter which is updated to note which
525 register was needed by this expression. OFFSET is added to the
526 register's value before it is pushed. */
529 pushf_register (int indent, string_file *stream,
530 unsigned char *registers_used,
531 struct gdbarch *gdbarch, int regnum, uint64_t offset)
533 std::string regname = compile_register_name_mangled (gdbarch, regnum);
535 registers_used[regnum] = 1;
537 pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
540 pushf (indent, stream,
541 COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s",
542 regname.c_str (), hex_string (offset));
545 /* Compile a DWARF expression to C code.
547 INDENT is the indentation level to use.
548 STREAM is the stream where the code should be written.
550 TYPE_NAME names the type of the result of the DWARF expression.
551 For locations this is "void *" but for array bounds it will be an
554 RESULT_NAME is the name of a variable in the resulting C code. The
555 result of the expression will be assigned to this variable.
557 SYM is the symbol corresponding to this expression.
558 PC is the location at which the expression is being evaluated.
559 ARCH is the architecture to use.
561 REGISTERS_USED is an out parameter which is updated to note which
562 registers were needed by this expression.
564 ADDR_SIZE is the DWARF address size to use.
566 OPT_PTR and OP_END are the bounds of the DWARF expression.
568 If non-NULL, INITIAL points to an initial value to write to the
569 stack. If NULL, no initial value is written.
571 PER_CU is the per-CU object used for looking up various other
575 do_compile_dwarf_expr_to_c (int indent, string_file *stream,
576 const char *type_name,
577 const char *result_name,
578 struct symbol *sym, CORE_ADDR pc,
579 struct gdbarch *arch,
580 unsigned char *registers_used,
581 unsigned int addr_size,
582 const gdb_byte *op_ptr, const gdb_byte *op_end,
584 struct dwarf2_per_cu_data *per_cu)
586 /* We keep a counter so that labels and other objects we create have
588 static unsigned int scope;
590 enum bfd_endian byte_order = gdbarch_byte_order (arch);
591 const gdb_byte * const base = op_ptr;
592 int need_tempvar = 0;
594 std::vector<struct insn_info> info;
599 fprintfi_filtered (indent, stream, "__attribute__ ((unused)) %s %s;\n",
600 type_name, result_name);
601 fprintfi_filtered (indent, stream, "{\n");
604 stack_depth = compute_stack_depth (byte_order, addr_size,
605 &need_tempvar, &is_tls,
606 op_ptr, op_end, initial != NULL,
609 /* This is a hack until we can add a feature to glibc to let us
610 properly generate code for TLS. You might think we could emit
611 the address in the ordinary course of translating
612 DW_OP_GNU_push_tls_address, but since the operand appears on the
613 stack, it is relatively hard to find, and the idea of calling
614 target_translate_tls_address with OFFSET==0 and then adding the
615 offset by hand seemed too hackish. */
618 struct frame_info *frame = get_selected_frame (NULL);
622 error (_("Symbol \"%s\" cannot be used because "
623 "there is no selected frame"),
624 SYMBOL_PRINT_NAME (sym));
626 val = read_var_value (sym, NULL, frame);
627 if (VALUE_LVAL (val) != lval_memory)
628 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
629 "as its address has not been found."),
630 SYMBOL_PRINT_NAME (sym));
632 warning (_("Symbol \"%s\" is thread-local and currently can only "
633 "be referenced from the current thread in "
635 SYMBOL_PRINT_NAME (sym));
637 fprintfi_filtered (indent, stream, "%s = %s;\n",
639 core_addr_to_string (value_address (val)));
640 fprintfi_filtered (indent - 2, stream, "}\n");
644 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_stack[%d];\n",
648 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_tmp;\n");
649 fprintfi_filtered (indent, stream, "int __gdb_tos = -1;\n");
652 pushf (indent, stream, "%s", core_addr_to_string (*initial));
654 while (op_ptr < op_end)
656 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
657 uint64_t uoffset, reg;
660 print_spaces (indent - 2, stream);
661 if (info[op_ptr - base].label)
663 print_label (stream, scope, op_ptr - base);
666 stream->printf ("/* %s */\n", get_DW_OP_name (op));
668 /* This is handy for debugging the generated code:
669 fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
670 (int) info[op_ptr - base].depth - 1);
709 push (indent, stream, op - DW_OP_lit0);
713 uoffset = extract_unsigned_integer (op_ptr, addr_size, byte_order);
715 /* Some versions of GCC emit DW_OP_addr before
716 DW_OP_GNU_push_tls_address. In this case the value is an
717 index, not an address. We don't support things like
718 branching between the address and the TLS op. */
719 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
720 uoffset += dwarf2_per_cu_text_offset (per_cu);
721 push (indent, stream, uoffset);
725 push (indent, stream,
726 extract_unsigned_integer (op_ptr, 1, byte_order));
730 push (indent, stream,
731 extract_signed_integer (op_ptr, 1, byte_order));
735 push (indent, stream,
736 extract_unsigned_integer (op_ptr, 2, byte_order));
740 push (indent, stream,
741 extract_signed_integer (op_ptr, 2, byte_order));
745 push (indent, stream,
746 extract_unsigned_integer (op_ptr, 4, byte_order));
750 push (indent, stream,
751 extract_signed_integer (op_ptr, 4, byte_order));
755 push (indent, stream,
756 extract_unsigned_integer (op_ptr, 8, byte_order));
760 push (indent, stream,
761 extract_signed_integer (op_ptr, 8, byte_order));
765 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
766 push (indent, stream, uoffset);
769 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
770 push (indent, stream, offset);
805 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
806 pushf_register_address (indent, stream, registers_used, arch,
807 dwarf_reg_to_regnum_or_error
808 (arch, op - DW_OP_reg0));
812 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
813 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
814 pushf_register_address (indent, stream, registers_used, arch,
815 dwarf_reg_to_regnum_or_error (arch, reg));
850 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
851 pushf_register (indent, stream, registers_used, arch,
852 dwarf_reg_to_regnum_or_error (arch,
858 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
859 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
860 pushf_register (indent, stream, registers_used, arch,
861 dwarf_reg_to_regnum_or_error (arch, reg), offset);
866 const gdb_byte *datastart;
868 const struct block *b;
869 struct symbol *framefunc;
872 b = block_for_pc (pc);
875 error (_("No block found for address"));
877 framefunc = block_linkage_function (b);
880 error (_("No function found for block"));
882 func_get_frame_base_dwarf_block (framefunc, pc,
883 &datastart, &datalen);
885 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
887 /* Generate a unique-enough name, in case the frame base
888 is computed multiple times in this expression. */
889 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
890 (long) (op_ptr - base));
892 do_compile_dwarf_expr_to_c (indent, stream,
893 GCC_UINTPTR, fb_name,
895 arch, registers_used, addr_size,
896 datastart, datastart + datalen,
899 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
904 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
908 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
913 pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
918 fprintfi_filtered (indent, stream,
919 "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
920 fprintfi_filtered (indent, stream,
921 "__gdb_stack[__gdb_tos - 1] = "
922 "__gdb_stack[__gdb_tos];\n");
923 fprintfi_filtered (indent, stream, ("__gdb_stack[__gdb_tos] = "
928 pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
932 fprintfi_filtered (indent, stream, ("__gdb_tmp = "
933 "__gdb_stack[__gdb_tos];\n"));
934 fprintfi_filtered (indent, stream,
935 "__gdb_stack[__gdb_tos] = "
936 "__gdb_stack[__gdb_tos - 1];\n");
937 fprintfi_filtered (indent, stream,
938 "__gdb_stack[__gdb_tos - 1] = "
939 "__gdb_stack[__gdb_tos -2];\n");
940 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 2] = "
945 case DW_OP_deref_size:
950 if (op == DW_OP_deref_size)
955 mode = c_get_mode_for_size (size);
957 error (_("Unsupported size %d in %s"),
958 size, get_DW_OP_name (op));
960 /* Cast to a pointer of the desired type, then
962 fprintfi_filtered (indent, stream,
963 "__gdb_stack[__gdb_tos] = "
964 "*((__gdb_int_%s *) "
965 "__gdb_stack[__gdb_tos]);\n",
971 unary (indent, stream,
972 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
973 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
977 unary (indent, stream, "-__gdb_stack[__gdb_tos]");
981 unary (indent, stream, "~__gdb_stack[__gdb_tos]");
984 case DW_OP_plus_uconst:
985 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
986 unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
991 binary (indent, stream, ("((" GCC_INTPTR
992 ") __gdb_stack[__gdb_tos-1]) / (("
993 GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
997 binary (indent, stream,
998 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
999 "__gdb_stack[__gdb_tos]");
1002 #define BINARY(OP) \
1003 binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1004 " __gdb_stack[__gdb_tos]"); \
1027 #define COMPARE(OP) \
1028 binary (indent, stream, \
1029 "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1031 ") __gdb_stack[__gdb_tos]))"); \
1048 case DW_OP_call_frame_cfa:
1051 CORE_ADDR text_offset;
1053 const gdb_byte *cfa_start, *cfa_end;
1055 if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1057 &text_offset, &cfa_start, &cfa_end))
1060 pushf_register (indent, stream, registers_used, arch, regnum,
1065 /* Another expression. */
1068 /* Generate a unique-enough name, in case the CFA is
1069 computed multiple times in this expression. */
1070 xsnprintf (cfa_name, sizeof (cfa_name),
1071 "__cfa_%ld", (long) (op_ptr - base));
1073 do_compile_dwarf_expr_to_c (indent, stream,
1074 GCC_UINTPTR, cfa_name,
1075 sym, pc, arch, registers_used,
1078 &text_offset, per_cu);
1079 pushf (indent, stream, "%s", cfa_name);
1086 offset = extract_signed_integer (op_ptr, 2, byte_order);
1088 fprintfi_filtered (indent, stream, "goto ");
1089 print_label (stream, scope, op_ptr + offset - base);
1090 stream->puts (";\n");
1094 offset = extract_signed_integer (op_ptr, 2, byte_order);
1096 fprintfi_filtered (indent, stream,
1097 "if ((( " GCC_INTPTR
1098 ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1099 print_label (stream, scope, op_ptr + offset - base);
1100 stream->puts (";\n");
1107 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1111 fprintfi_filtered (indent, stream, "%s = __gdb_stack[__gdb_tos];\n",
1113 fprintfi_filtered (indent - 2, stream, "}\n");
1116 /* See compile.h. */
1119 compile_dwarf_expr_to_c (string_file *stream, const char *result_name,
1120 struct symbol *sym, CORE_ADDR pc,
1121 struct gdbarch *arch, unsigned char *registers_used,
1122 unsigned int addr_size,
1123 const gdb_byte *op_ptr, const gdb_byte *op_end,
1124 struct dwarf2_per_cu_data *per_cu)
1126 do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1127 arch, registers_used, addr_size, op_ptr, op_end,
1131 /* See compile.h. */
1134 compile_dwarf_bounds_to_c (string_file *stream,
1135 const char *result_name,
1136 const struct dynamic_prop *prop,
1137 struct symbol *sym, CORE_ADDR pc,
1138 struct gdbarch *arch, unsigned char *registers_used,
1139 unsigned int addr_size,
1140 const gdb_byte *op_ptr, const gdb_byte *op_end,
1141 struct dwarf2_per_cu_data *per_cu)
1143 do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1144 sym, pc, arch, registers_used,
1145 addr_size, op_ptr, op_end, NULL, per_cu);