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
446 static void pushf (int indent, struct ui_file *stream, const char *format, ...)
447 ATTRIBUTE_PRINTF (3, 4);
450 pushf (int indent, struct ui_file *stream, const char *format, ...)
454 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos + 1] = ");
455 va_start (args, format);
456 vfprintf_filtered (stream, format, args);
458 fprintf_filtered (stream, ";\n");
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, struct ui_file *stream, const char *format, ...)
467 ATTRIBUTE_PRINTF (3, 4);
470 unary (int indent, struct ui_file *stream, const char *format, ...)
474 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos] = ");
475 va_start (args, format);
476 vfprintf_filtered (stream, format, args);
478 fprintf_filtered (stream, ";\n");
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, struct ui_file *stream, const char *format, ...)
484 ATTRIBUTE_PRINTF (3, 4);
487 binary (int indent, struct ui_file *stream, const char *format, ...)
491 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 1] = ");
492 va_start (args, format);
493 vfprintf_filtered (stream, format, args);
495 fprintf_filtered (stream, ";\n");
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 (struct ui_file *stream, unsigned int scope, int target)
506 fprintf_filtered (stream, "__label_%u_%s",
507 scope, pulongest (target));
510 /* Emit code that pushes a register's address on the stack.
511 REGISTERS_USED is an out parameter which is updated to note which
512 register was needed by this expression. */
515 pushf_register_address (int indent, struct ui_file *stream,
516 unsigned char *registers_used,
517 struct gdbarch *gdbarch, int regnum)
519 char *regname = compile_register_name_mangled (gdbarch, regnum);
520 struct cleanup *cleanups = make_cleanup (xfree, regname);
522 registers_used[regnum] = 1;
523 pushf (indent, stream, "&" 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, struct ui_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, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + %s",
548 regname, hex_string (offset));
550 do_cleanups (cleanups);
553 /* Compile a DWARF expression to C code.
555 INDENT is the indentation level to use.
556 STREAM is the stream where the code should be written.
558 TYPE_NAME names the type of the result of the DWARF expression.
559 For locations this is "void *" but for array bounds it will be an
562 RESULT_NAME is the name of a variable in the resulting C code. The
563 result of the expression will be assigned to this variable.
565 SYM is the symbol corresponding to this expression.
566 PC is the location at which the expression is being evaluated.
567 ARCH is the architecture to use.
569 REGISTERS_USED is an out parameter which is updated to note which
570 registers were needed by this expression.
572 ADDR_SIZE is the DWARF address size to use.
574 OPT_PTR and OP_END are the bounds of the DWARF expression.
576 If non-NULL, INITIAL points to an initial value to write to the
577 stack. If NULL, no initial value is written.
579 PER_CU is the per-CU object used for looking up various other
583 do_compile_dwarf_expr_to_c (int indent, struct ui_file *stream,
584 const char *type_name,
585 const char *result_name,
586 struct symbol *sym, CORE_ADDR pc,
587 struct gdbarch *arch,
588 unsigned char *registers_used,
589 unsigned int addr_size,
590 const gdb_byte *op_ptr, const gdb_byte *op_end,
592 struct dwarf2_per_cu_data *per_cu)
594 /* We keep a counter so that labels and other objects we create have
596 static unsigned int scope;
598 enum bfd_endian byte_order = gdbarch_byte_order (arch);
599 const gdb_byte * const base = op_ptr;
600 int need_tempvar = 0;
602 struct cleanup *cleanup;
603 struct insn_info *info;
608 fprintfi_filtered (indent, stream, "%s%s;\n", type_name, result_name);
609 fprintfi_filtered (indent, stream, "{\n");
612 stack_depth = compute_stack_depth (byte_order, addr_size,
613 &need_tempvar, &is_tls,
614 op_ptr, op_end, initial != NULL,
616 cleanup = make_cleanup (xfree, info);
618 /* This is a hack until we can add a feature to glibc to let us
619 properly generate code for TLS. You might think we could emit
620 the address in the ordinary course of translating
621 DW_OP_GNU_push_tls_address, but since the operand appears on the
622 stack, it is relatively hard to find, and the idea of calling
623 target_translate_tls_address with OFFSET==0 and then adding the
624 offset by hand seemed too hackish. */
627 struct frame_info *frame = get_selected_frame (NULL);
631 error (_("Symbol \"%s\" cannot be used because "
632 "there is no selected frame"),
633 SYMBOL_PRINT_NAME (sym));
635 val = read_var_value (sym, frame);
636 if (VALUE_LVAL (val) != lval_memory)
637 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
638 "as its address has not been found."),
639 SYMBOL_PRINT_NAME (sym));
641 warning (_("Symbol \"%s\" is thread-local and currently can only "
642 "be referenced from the current thread in "
644 SYMBOL_PRINT_NAME (sym));
646 fprintfi_filtered (indent, stream, "%s = %s;\n",
648 core_addr_to_string (value_address (val)));
649 fprintfi_filtered (indent - 2, stream, "}\n");
650 do_cleanups (cleanup);
654 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_stack[%d];\n",
658 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_tmp;\n");
659 fprintfi_filtered (indent, stream, "int __gdb_tos = -1;\n");
662 pushf (indent, stream, "%s", core_addr_to_string (*initial));
664 while (op_ptr < op_end)
666 enum dwarf_location_atom op = *op_ptr;
667 uint64_t uoffset, reg;
670 print_spaces (indent - 2, stream);
671 if (info[op_ptr - base].label)
673 print_label (stream, scope, op_ptr - base);
674 fprintf_filtered (stream, ":;");
676 fprintf_filtered (stream, "/* %s */\n", get_DW_OP_name (op));
678 /* This is handy for debugging the generated code:
679 fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
680 (int) info[op_ptr - base].depth - 1);
719 push (indent, stream, op - DW_OP_lit0);
724 /* Some versions of GCC emit DW_OP_addr before
725 DW_OP_GNU_push_tls_address. In this case the value is an
726 index, not an address. We don't support things like
727 branching between the address and the TLS op. */
728 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
729 uoffset += dwarf2_per_cu_text_offset (per_cu);
730 push (indent, stream, uoffset);
734 push (indent, stream,
735 extract_unsigned_integer (op_ptr, 1, byte_order));
739 push (indent, stream,
740 extract_signed_integer (op_ptr, 1, byte_order));
744 push (indent, stream,
745 extract_unsigned_integer (op_ptr, 2, byte_order));
749 push (indent, stream,
750 extract_signed_integer (op_ptr, 2, byte_order));
754 push (indent, stream,
755 extract_unsigned_integer (op_ptr, 4, byte_order));
759 push (indent, stream,
760 extract_signed_integer (op_ptr, 4, byte_order));
764 push (indent, stream,
765 extract_unsigned_integer (op_ptr, 8, byte_order));
769 push (indent, stream,
770 extract_signed_integer (op_ptr, 8, byte_order));
774 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
775 push (indent, stream, uoffset);
778 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
779 push (indent, stream, offset);
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,
821 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
822 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
823 pushf_register_address (indent, stream, registers_used, arch,
824 dwarf2_reg_to_regnum_or_error (arch, reg));
859 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
860 pushf_register (indent, stream, registers_used, arch,
861 dwarf2_reg_to_regnum_or_error (arch,
867 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
868 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
869 pushf_register (indent, stream, registers_used, arch,
870 dwarf2_reg_to_regnum_or_error (arch, reg), offset);
875 const gdb_byte *datastart;
877 const struct block *b;
878 struct symbol *framefunc;
881 b = block_for_pc (pc);
884 error (_("No block found for address"));
886 framefunc = block_linkage_function (b);
889 error (_("No function found for block"));
891 func_get_frame_base_dwarf_block (framefunc, pc,
892 &datastart, &datalen);
894 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
896 /* Generate a unique-enough name, in case the frame base
897 is computed multiple times in this expression. */
898 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
899 (long) (op_ptr - base));
901 do_compile_dwarf_expr_to_c (indent, stream,
904 arch, registers_used, addr_size,
905 datastart, datastart + datalen,
908 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
913 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
917 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
922 pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
927 fprintfi_filtered (indent, stream,
928 "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
929 fprintfi_filtered (indent, stream,
930 "__gdb_stack[__gdb_tos - 1] = "
931 "__gdb_stack[__gdb_tos];\n");
932 fprintfi_filtered (indent, stream, ("__gdb_stack[__gdb_tos] = "
937 pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
941 fprintfi_filtered (indent, stream, ("__gdb_tmp = "
942 "__gdb_stack[__gdb_tos];\n"));
943 fprintfi_filtered (indent, stream,
944 "__gdb_stack[__gdb_tos] = "
945 "__gdb_stack[__gdb_tos - 1];\n");
946 fprintfi_filtered (indent, stream,
947 "__gdb_stack[__gdb_tos - 1] = "
948 "__gdb_stack[__gdb_tos -2];\n");
949 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 2] = "
954 case DW_OP_deref_size:
959 if (op == DW_OP_deref_size)
964 mode = c_get_mode_for_size (size);
966 error (_("Unsupported size %d in %s"),
967 size, get_DW_OP_name (op));
969 /* Cast to a pointer of the desired type, then
971 fprintfi_filtered (indent, stream,
972 "__gdb_stack[__gdb_tos] = "
973 "*((__gdb_int_%s *) "
974 "__gdb_stack[__gdb_tos]);\n",
980 unary (indent, stream,
981 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
982 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
986 unary (indent, stream, "-__gdb_stack[__gdb_tos]");
990 unary (indent, stream, "~__gdb_stack[__gdb_tos]");
993 case DW_OP_plus_uconst:
994 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
995 unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
1000 binary (indent, stream, ("((" GCC_INTPTR
1001 ") __gdb_stack[__gdb_tos-1]) / (("
1002 GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
1006 binary (indent, stream,
1007 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
1008 "__gdb_stack[__gdb_tos]");
1011 #define BINARY(OP) \
1012 binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1013 " __gdb_stack[__gdb_tos]"); \
1036 #define COMPARE(OP) \
1037 binary (indent, stream, \
1038 "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1040 ") __gdb_stack[__gdb_tos]))"); \
1057 case DW_OP_call_frame_cfa:
1060 CORE_ADDR text_offset;
1062 const gdb_byte *cfa_start, *cfa_end;
1064 if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1066 &text_offset, &cfa_start, &cfa_end))
1069 pushf_register (indent, stream, registers_used, arch, regnum,
1074 /* Another expression. */
1077 /* Generate a unique-enough name, in case the CFA is
1078 computed multiple times in this expression. */
1079 xsnprintf (cfa_name, sizeof (cfa_name),
1080 "__cfa_%ld", (long) (op_ptr - base));
1082 do_compile_dwarf_expr_to_c (indent, stream,
1084 sym, pc, arch, registers_used,
1087 &text_offset, per_cu);
1088 pushf (indent, stream, "%s", cfa_name);
1095 offset = extract_signed_integer (op_ptr, 2, byte_order);
1097 fprintfi_filtered (indent, stream, "goto ");
1098 print_label (stream, scope, op_ptr + offset - base);
1099 fprintf_filtered (stream, ";\n");
1103 offset = extract_signed_integer (op_ptr, 2, byte_order);
1105 fprintfi_filtered (indent, stream,
1106 "if ((( " GCC_INTPTR
1107 ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1108 print_label (stream, scope, op_ptr + offset - base);
1109 fprintf_filtered (stream, ";\n");
1116 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1120 fprintfi_filtered (indent, stream, "%s = (%s) __gdb_stack[__gdb_tos];\n",
1121 result_name, type_name);
1122 fprintfi_filtered (indent - 2, stream, "}\n");
1124 do_cleanups (cleanup);
1127 /* See compile.h. */
1130 compile_dwarf_expr_to_c (struct ui_file *stream, const char *result_name,
1131 struct symbol *sym, CORE_ADDR pc,
1132 struct gdbarch *arch, unsigned char *registers_used,
1133 unsigned int addr_size,
1134 const gdb_byte *op_ptr, const gdb_byte *op_end,
1135 struct dwarf2_per_cu_data *per_cu)
1137 do_compile_dwarf_expr_to_c (2, stream, "void *", result_name, sym, pc,
1138 arch, registers_used, addr_size, op_ptr, op_end,
1142 /* See compile.h. */
1145 compile_dwarf_bounds_to_c (struct ui_file *stream,
1146 const char *result_name,
1147 const struct dynamic_prop *prop,
1148 struct symbol *sym, CORE_ADDR pc,
1149 struct gdbarch *arch, unsigned char *registers_used,
1150 unsigned int addr_size,
1151 const gdb_byte *op_ptr, const gdb_byte *op_end,
1152 struct dwarf2_per_cu_data *per_cu)
1154 do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1155 sym, pc, arch, registers_used,
1156 addr_size, op_ptr, op_end, NULL, per_cu);