1 /* Convert a DWARF location expression to C
3 Copyright (C) 2014-2015 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. This is
52 a hack until we can add a feature to glibc to let us properly
53 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 = *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 info[ndx].is_tls = 1;
330 offset = extract_signed_integer (op_ptr, 2, byte_order);
332 offset = op_ptr + offset - base;
333 /* If the destination has not been seen yet, add it to the
335 if (!info[offset].visited)
336 VEC_safe_push (int, *to_do, offset);
337 SET_CHECK_DEPTH (offset);
338 info[offset].label = 1;
339 /* We're done with this line of code. */
343 offset = extract_signed_integer (op_ptr, 2, byte_order);
345 offset = op_ptr + offset - base;
347 /* If the destination has not been seen yet, add it to the
349 if (!info[offset].visited)
350 VEC_safe_push (int, *to_do, offset);
351 SET_CHECK_DEPTH (offset);
352 info[offset].label = 1;
359 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
363 gdb_assert (op_ptr == op_end);
365 #undef SET_CHECK_DEPTH
368 /* Compute the maximum needed stack depth of a DWARF expression, and
369 some other information as well.
371 BYTE_ORDER and ADDR_SIZE describe this bytecode in the obvious way.
372 NEED_TEMPVAR is an out parameter which is set if this expression
373 needs a special temporary variable to be emitted (see the code
375 IS_TLS is an out parameter which is set if this expression refers
377 OP_PTR and OP_END are the bounds of the DWARF expression.
378 INITIAL_DEPTH is the initial depth of the DWARF expression stack.
379 INFO is an array of insn_info objects, indexed by offset from the
380 start of the DWARF expression.
382 This returns the maximum stack depth. */
385 compute_stack_depth (enum bfd_endian byte_order, unsigned int addr_size,
386 int *need_tempvar, int *is_tls,
387 const gdb_byte *op_ptr, const gdb_byte *op_end,
389 struct insn_info **info)
392 struct cleanup *outer_cleanup, *cleanup;
393 VEC (int) *to_do = NULL;
396 *info = XCNEWVEC (struct insn_info, op_end - op_ptr);
397 outer_cleanup = make_cleanup (xfree, *info);
399 cleanup = make_cleanup (VEC_cleanup (int), &to_do);
401 VEC_safe_push (int, to_do, 0);
402 (*info)[0].depth = initial_depth;
403 (*info)[0].visited = 1;
405 while (!VEC_empty (int, to_do))
407 int ndx = VEC_pop (int, to_do);
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 do_cleanups (cleanup);
425 discard_cleanups (outer_cleanup);
426 return stack_depth + 1;
431 #define GCC_UINTPTR "__gdb_uintptr"
432 #define GCC_INTPTR "__gdb_intptr"
434 /* Emit code to push a constant. */
437 push (int indent, struct ui_file *stream, ULONGEST l)
439 fprintfi_filtered (indent, stream, "__gdb_stack[++__gdb_tos] = %s;\n",
443 /* Emit code to push an arbitrary expression. This works like
447 pushf (int indent, struct ui_file *stream, const char *format, ...)
451 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos + 1] = ");
452 va_start (args, format);
453 vfprintf_filtered (stream, format, args);
455 fprintf_filtered (stream, ";\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. */
464 unary (int indent, struct ui_file *stream, const char *format, ...)
468 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos] = ");
469 va_start (args, format);
470 vfprintf_filtered (stream, format, args);
472 fprintf_filtered (stream, ";\n");
475 /* Emit code for a unary expression -- one which uses the top two
476 stack items, popping the topmost one. This works like printf. */
479 binary (int indent, struct ui_file *stream, const char *format, ...)
483 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 1] = ");
484 va_start (args, format);
485 vfprintf_filtered (stream, format, args);
487 fprintf_filtered (stream, ";\n");
488 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
491 /* Print the name of a label given its "SCOPE", an arbitrary integer
492 used for uniqueness, and its TARGET, the bytecode offset
493 corresponding to the label's point of definition. */
496 print_label (struct ui_file *stream, unsigned int scope, int target)
498 fprintf_filtered (stream, "__label_%u_%s",
499 scope, pulongest (target));
502 /* Emit code that pushes a register's address on the stack.
503 REGISTERS_USED is an out parameter which is updated to note which
504 register was needed by this expression. */
507 pushf_register_address (int indent, struct ui_file *stream,
508 unsigned char *registers_used,
509 struct gdbarch *gdbarch, int regnum)
511 char *regname = compile_register_name_mangled (gdbarch, regnum);
512 struct cleanup *cleanups = make_cleanup (xfree, regname);
514 registers_used[regnum] = 1;
515 pushf (indent, stream, "&" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
518 do_cleanups (cleanups);
521 /* Emit code that pushes a register's value on the stack.
522 REGISTERS_USED is an out parameter which is updated to note which
523 register was needed by this expression. OFFSET is added to the
524 register's value before it is pushed. */
527 pushf_register (int indent, struct ui_file *stream,
528 unsigned char *registers_used,
529 struct gdbarch *gdbarch, int regnum, uint64_t offset)
531 char *regname = compile_register_name_mangled (gdbarch, regnum);
532 struct cleanup *cleanups = make_cleanup (xfree, regname);
534 registers_used[regnum] = 1;
536 pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
539 pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + %s",
540 regname, hex_string (offset));
542 do_cleanups (cleanups);
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, struct ui_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 struct cleanup *cleanup;
595 struct insn_info *info;
600 fprintfi_filtered (indent, stream, "%s%s;\n", 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,
608 cleanup = make_cleanup (xfree, info);
610 /* This is a hack until we can add a feature to glibc to let us
611 properly generate code for TLS. You might think we could emit
612 the address in the ordinary course of translating
613 DW_OP_GNU_push_tls_address, but since the operand appears on the
614 stack, it is relatively hard to find, and the idea of calling
615 target_translate_tls_address with OFFSET==0 and then adding the
616 offset by hand seemed too hackish. */
619 struct frame_info *frame = get_selected_frame (NULL);
623 error (_("Symbol \"%s\" cannot be used because "
624 "there is no selected frame"),
625 SYMBOL_PRINT_NAME (sym));
627 val = read_var_value (sym, frame);
628 if (VALUE_LVAL (val) != lval_memory)
629 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
630 "as its address has not been found."),
631 SYMBOL_PRINT_NAME (sym));
633 warning (_("Symbol \"%s\" is thread-local and currently can only "
634 "be referenced from the current thread in "
636 SYMBOL_PRINT_NAME (sym));
638 fprintfi_filtered (indent, stream, "%s = %s;\n",
640 core_addr_to_string (value_address (val)));
641 fprintfi_filtered (indent - 2, stream, "}\n");
642 do_cleanups (cleanup);
646 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_stack[%d];\n",
650 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_tmp;\n");
651 fprintfi_filtered (indent, stream, "int __gdb_tos = -1;\n");
654 pushf (indent, stream, core_addr_to_string (*initial));
656 while (op_ptr < op_end)
658 enum dwarf_location_atom op = *op_ptr;
659 uint64_t uoffset, reg;
662 print_spaces (indent - 2, stream);
663 if (info[op_ptr - base].label)
665 print_label (stream, scope, op_ptr - base);
666 fprintf_filtered (stream, ":;");
668 fprintf_filtered (stream, "/* %s */\n", get_DW_OP_name (op));
670 /* This is handy for debugging the generated code:
671 fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
672 (int) info[op_ptr - base].depth - 1);
711 push (indent, stream, op - DW_OP_lit0);
716 /* Some versions of GCC emit DW_OP_addr before
717 DW_OP_GNU_push_tls_address. In this case the value is an
718 index, not an address. We don't support things like
719 branching between the address and the TLS op. */
720 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
721 uoffset += dwarf2_per_cu_text_offset (per_cu);
722 push (indent, stream, uoffset);
726 push (indent, stream,
727 extract_unsigned_integer (op_ptr, 1, byte_order));
731 push (indent, stream,
732 extract_signed_integer (op_ptr, 1, byte_order));
736 push (indent, stream,
737 extract_unsigned_integer (op_ptr, 2, byte_order));
741 push (indent, stream,
742 extract_signed_integer (op_ptr, 2, byte_order));
746 push (indent, stream,
747 extract_unsigned_integer (op_ptr, 4, byte_order));
751 push (indent, stream,
752 extract_signed_integer (op_ptr, 4, byte_order));
756 push (indent, stream,
757 extract_unsigned_integer (op_ptr, 8, byte_order));
761 push (indent, stream,
762 extract_signed_integer (op_ptr, 8, byte_order));
766 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
767 push (indent, stream, uoffset);
770 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
771 push (indent, stream, offset);
806 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
807 pushf_register_address (indent, stream, registers_used, arch,
808 dwarf2_reg_to_regnum_or_error (arch,
813 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
814 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
815 pushf_register_address (indent, stream, registers_used, arch,
816 dwarf2_reg_to_regnum_or_error (arch, reg));
851 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
852 pushf_register (indent, stream, registers_used, arch,
853 dwarf2_reg_to_regnum_or_error (arch,
859 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
860 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
861 pushf_register (indent, stream, registers_used, arch,
862 dwarf2_reg_to_regnum_or_error (arch, reg), offset);
867 const gdb_byte *datastart;
869 const struct block *b;
870 struct symbol *framefunc;
873 b = block_for_pc (pc);
876 error (_("No block found for address"));
878 framefunc = block_linkage_function (b);
881 error (_("No function found for block"));
883 func_get_frame_base_dwarf_block (framefunc, pc,
884 &datastart, &datalen);
886 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
888 /* Generate a unique-enough name, in case the frame base
889 is computed multiple times in this expression. */
890 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
891 (long) (op_ptr - base));
893 do_compile_dwarf_expr_to_c (indent, stream,
896 arch, registers_used, addr_size,
897 datastart, datastart + datalen,
900 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
905 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
909 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
914 pushf (indent, stream, "__gdb_stack[__gdb_tos - %d]", offset);
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, ("__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,
1075 sym, pc, arch, registers_used,
1078 &text_offset, per_cu);
1079 pushf (indent, stream, 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 fprintf_filtered (stream, ";\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 fprintf_filtered (stream, ";\n");
1107 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1111 fprintfi_filtered (indent, stream, "%s = (%s) __gdb_stack[__gdb_tos];\n",
1112 result_name, type_name);
1113 fprintfi_filtered (indent - 2, stream, "}\n");
1115 do_cleanups (cleanup);
1118 /* See compile.h. */
1121 compile_dwarf_expr_to_c (struct ui_file *stream, const char *result_name,
1122 struct symbol *sym, CORE_ADDR pc,
1123 struct gdbarch *arch, unsigned char *registers_used,
1124 unsigned int addr_size,
1125 const gdb_byte *op_ptr, const gdb_byte *op_end,
1126 struct dwarf2_per_cu_data *per_cu)
1128 do_compile_dwarf_expr_to_c (2, stream, "void *", result_name, sym, pc,
1129 arch, registers_used, addr_size, op_ptr, op_end,
1133 /* See compile.h. */
1136 compile_dwarf_bounds_to_c (struct ui_file *stream,
1137 const char *result_name,
1138 const struct dynamic_prop *prop,
1139 struct symbol *sym, CORE_ADDR pc,
1140 struct gdbarch *arch, unsigned char *registers_used,
1141 unsigned int addr_size,
1142 const gdb_byte *op_ptr, const gdb_byte *op_end,
1143 struct dwarf2_per_cu_data *per_cu)
1145 do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1146 sym, pc, arch, registers_used,
1147 addr_size, op_ptr, op_end, NULL, per_cu);