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 = (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 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,
440 "__gdb_stack[++__gdb_tos] = (" GCC_UINTPTR ") %s;\n",
444 /* Emit code to push an arbitrary expression. This works like
447 static void pushf (int indent, struct ui_file *stream, const char *format, ...)
448 ATTRIBUTE_PRINTF (3, 4);
451 pushf (int indent, struct ui_file *stream, const char *format, ...)
455 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos + 1] = ");
456 va_start (args, format);
457 vfprintf_filtered (stream, format, args);
459 fprintf_filtered (stream, ";\n");
461 fprintfi_filtered (indent, stream, "++__gdb_tos;\n");
464 /* Emit code for a unary expression -- one which operates in-place on
465 the top-of-stack. This works like printf. */
467 static void unary (int indent, struct ui_file *stream, const char *format, ...)
468 ATTRIBUTE_PRINTF (3, 4);
471 unary (int indent, struct ui_file *stream, const char *format, ...)
475 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos] = ");
476 va_start (args, format);
477 vfprintf_filtered (stream, format, args);
479 fprintf_filtered (stream, ";\n");
482 /* Emit code for a unary expression -- one which uses the top two
483 stack items, popping the topmost one. This works like printf. */
484 static void binary (int indent, struct ui_file *stream, const char *format, ...)
485 ATTRIBUTE_PRINTF (3, 4);
488 binary (int indent, struct ui_file *stream, const char *format, ...)
492 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 1] = ");
493 va_start (args, format);
494 vfprintf_filtered (stream, format, args);
496 fprintf_filtered (stream, ";\n");
497 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
500 /* Print the name of a label given its "SCOPE", an arbitrary integer
501 used for uniqueness, and its TARGET, the bytecode offset
502 corresponding to the label's point of definition. */
505 print_label (struct ui_file *stream, unsigned int scope, int target)
507 fprintf_filtered (stream, "__label_%u_%s",
508 scope, pulongest (target));
511 /* Emit code that pushes a register's address on the stack.
512 REGISTERS_USED is an out parameter which is updated to note which
513 register was needed by this expression. */
516 pushf_register_address (int indent, struct ui_file *stream,
517 unsigned char *registers_used,
518 struct gdbarch *gdbarch, int regnum)
520 char *regname = compile_register_name_mangled (gdbarch, regnum);
521 struct cleanup *cleanups = make_cleanup (xfree, regname);
523 registers_used[regnum] = 1;
524 pushf (indent, stream,
525 "(" GCC_UINTPTR ") &" COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
528 do_cleanups (cleanups);
531 /* Emit code that pushes a register's value on the stack.
532 REGISTERS_USED is an out parameter which is updated to note which
533 register was needed by this expression. OFFSET is added to the
534 register's value before it is pushed. */
537 pushf_register (int indent, struct ui_file *stream,
538 unsigned char *registers_used,
539 struct gdbarch *gdbarch, int regnum, uint64_t offset)
541 char *regname = compile_register_name_mangled (gdbarch, regnum);
542 struct cleanup *cleanups = make_cleanup (xfree, regname);
544 registers_used[regnum] = 1;
546 pushf (indent, stream, COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s",
549 pushf (indent, stream,
550 COMPILE_I_SIMPLE_REGISTER_ARG_NAME "->%s + (" GCC_UINTPTR ") %s",
551 regname, hex_string (offset));
553 do_cleanups (cleanups);
556 /* Compile a DWARF expression to C code.
558 INDENT is the indentation level to use.
559 STREAM is the stream where the code should be written.
561 TYPE_NAME names the type of the result of the DWARF expression.
562 For locations this is "void *" but for array bounds it will be an
565 RESULT_NAME is the name of a variable in the resulting C code. The
566 result of the expression will be assigned to this variable.
568 SYM is the symbol corresponding to this expression.
569 PC is the location at which the expression is being evaluated.
570 ARCH is the architecture to use.
572 REGISTERS_USED is an out parameter which is updated to note which
573 registers were needed by this expression.
575 ADDR_SIZE is the DWARF address size to use.
577 OPT_PTR and OP_END are the bounds of the DWARF expression.
579 If non-NULL, INITIAL points to an initial value to write to the
580 stack. If NULL, no initial value is written.
582 PER_CU is the per-CU object used for looking up various other
586 do_compile_dwarf_expr_to_c (int indent, struct ui_file *stream,
587 const char *type_name,
588 const char *result_name,
589 struct symbol *sym, CORE_ADDR pc,
590 struct gdbarch *arch,
591 unsigned char *registers_used,
592 unsigned int addr_size,
593 const gdb_byte *op_ptr, const gdb_byte *op_end,
595 struct dwarf2_per_cu_data *per_cu)
597 /* We keep a counter so that labels and other objects we create have
599 static unsigned int scope;
601 enum bfd_endian byte_order = gdbarch_byte_order (arch);
602 const gdb_byte * const base = op_ptr;
603 int need_tempvar = 0;
605 struct cleanup *cleanup;
606 struct insn_info *info;
611 fprintfi_filtered (indent, stream, "__attribute__ ((unused)) %s %s;\n",
612 type_name, result_name);
613 fprintfi_filtered (indent, stream, "{\n");
616 stack_depth = compute_stack_depth (byte_order, addr_size,
617 &need_tempvar, &is_tls,
618 op_ptr, op_end, initial != NULL,
620 cleanup = make_cleanup (xfree, info);
622 /* This is a hack until we can add a feature to glibc to let us
623 properly generate code for TLS. You might think we could emit
624 the address in the ordinary course of translating
625 DW_OP_GNU_push_tls_address, but since the operand appears on the
626 stack, it is relatively hard to find, and the idea of calling
627 target_translate_tls_address with OFFSET==0 and then adding the
628 offset by hand seemed too hackish. */
631 struct frame_info *frame = get_selected_frame (NULL);
635 error (_("Symbol \"%s\" cannot be used because "
636 "there is no selected frame"),
637 SYMBOL_PRINT_NAME (sym));
639 val = read_var_value (sym, NULL, frame);
640 if (VALUE_LVAL (val) != lval_memory)
641 error (_("Symbol \"%s\" cannot be used for compilation evaluation "
642 "as its address has not been found."),
643 SYMBOL_PRINT_NAME (sym));
645 warning (_("Symbol \"%s\" is thread-local and currently can only "
646 "be referenced from the current thread in "
648 SYMBOL_PRINT_NAME (sym));
650 fprintfi_filtered (indent, stream, "%s = %s;\n",
652 core_addr_to_string (value_address (val)));
653 fprintfi_filtered (indent - 2, stream, "}\n");
654 do_cleanups (cleanup);
658 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_stack[%d];\n",
662 fprintfi_filtered (indent, stream, GCC_UINTPTR " __gdb_tmp;\n");
663 fprintfi_filtered (indent, stream, "int __gdb_tos = -1;\n");
666 pushf (indent, stream, "%s", core_addr_to_string (*initial));
668 while (op_ptr < op_end)
670 enum dwarf_location_atom op = (enum dwarf_location_atom) *op_ptr;
671 uint64_t uoffset, reg;
674 print_spaces (indent - 2, stream);
675 if (info[op_ptr - base].label)
677 print_label (stream, scope, op_ptr - base);
678 fprintf_filtered (stream, ":;");
680 fprintf_filtered (stream, "/* %s */\n", get_DW_OP_name (op));
682 /* This is handy for debugging the generated code:
683 fprintf_filtered (stream, "if (__gdb_tos != %d) abort ();\n",
684 (int) info[op_ptr - base].depth - 1);
723 push (indent, stream, op - DW_OP_lit0);
728 /* Some versions of GCC emit DW_OP_addr before
729 DW_OP_GNU_push_tls_address. In this case the value is an
730 index, not an address. We don't support things like
731 branching between the address and the TLS op. */
732 if (op_ptr >= op_end || *op_ptr != DW_OP_GNU_push_tls_address)
733 uoffset += dwarf2_per_cu_text_offset (per_cu);
734 push (indent, stream, uoffset);
738 push (indent, stream,
739 extract_unsigned_integer (op_ptr, 1, byte_order));
743 push (indent, stream,
744 extract_signed_integer (op_ptr, 1, byte_order));
748 push (indent, stream,
749 extract_unsigned_integer (op_ptr, 2, byte_order));
753 push (indent, stream,
754 extract_signed_integer (op_ptr, 2, byte_order));
758 push (indent, stream,
759 extract_unsigned_integer (op_ptr, 4, byte_order));
763 push (indent, stream,
764 extract_signed_integer (op_ptr, 4, byte_order));
768 push (indent, stream,
769 extract_unsigned_integer (op_ptr, 8, byte_order));
773 push (indent, stream,
774 extract_signed_integer (op_ptr, 8, byte_order));
778 op_ptr = safe_read_uleb128 (op_ptr, op_end, &uoffset);
779 push (indent, stream, uoffset);
782 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
783 push (indent, stream, offset);
818 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
819 pushf_register_address (indent, stream, registers_used, arch,
820 dwarf_reg_to_regnum_or_error
821 (arch, op - DW_OP_reg0));
825 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
826 dwarf_expr_require_composition (op_ptr, op_end, "DW_OP_regx");
827 pushf_register_address (indent, stream, registers_used, arch,
828 dwarf_reg_to_regnum_or_error (arch, reg));
863 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
864 pushf_register (indent, stream, registers_used, arch,
865 dwarf_reg_to_regnum_or_error (arch,
871 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
872 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
873 pushf_register (indent, stream, registers_used, arch,
874 dwarf_reg_to_regnum_or_error (arch, reg), offset);
879 const gdb_byte *datastart;
881 const struct block *b;
882 struct symbol *framefunc;
885 b = block_for_pc (pc);
888 error (_("No block found for address"));
890 framefunc = block_linkage_function (b);
893 error (_("No function found for block"));
895 func_get_frame_base_dwarf_block (framefunc, pc,
896 &datastart, &datalen);
898 op_ptr = safe_read_sleb128 (op_ptr, op_end, &offset);
900 /* Generate a unique-enough name, in case the frame base
901 is computed multiple times in this expression. */
902 xsnprintf (fb_name, sizeof (fb_name), "__frame_base_%ld",
903 (long) (op_ptr - base));
905 do_compile_dwarf_expr_to_c (indent, stream,
906 GCC_UINTPTR, fb_name,
908 arch, registers_used, addr_size,
909 datastart, datastart + datalen,
912 pushf (indent, stream, "%s + %s", fb_name, hex_string (offset));
917 pushf (indent, stream, "__gdb_stack[__gdb_tos]");
921 fprintfi_filtered (indent, stream, "--__gdb_tos;\n");
926 pushf (indent, stream, "__gdb_stack[__gdb_tos - %s]",
931 fprintfi_filtered (indent, stream,
932 "__gdb_tmp = __gdb_stack[__gdb_tos - 1];\n");
933 fprintfi_filtered (indent, stream,
934 "__gdb_stack[__gdb_tos - 1] = "
935 "__gdb_stack[__gdb_tos];\n");
936 fprintfi_filtered (indent, stream, ("__gdb_stack[__gdb_tos] = "
941 pushf (indent, stream, "__gdb_stack[__gdb_tos - 1]");
945 fprintfi_filtered (indent, stream, ("__gdb_tmp = "
946 "__gdb_stack[__gdb_tos];\n"));
947 fprintfi_filtered (indent, stream,
948 "__gdb_stack[__gdb_tos] = "
949 "__gdb_stack[__gdb_tos - 1];\n");
950 fprintfi_filtered (indent, stream,
951 "__gdb_stack[__gdb_tos - 1] = "
952 "__gdb_stack[__gdb_tos -2];\n");
953 fprintfi_filtered (indent, stream, "__gdb_stack[__gdb_tos - 2] = "
958 case DW_OP_deref_size:
963 if (op == DW_OP_deref_size)
968 mode = c_get_mode_for_size (size);
970 error (_("Unsupported size %d in %s"),
971 size, get_DW_OP_name (op));
973 /* Cast to a pointer of the desired type, then
975 fprintfi_filtered (indent, stream,
976 "__gdb_stack[__gdb_tos] = "
977 "*((__gdb_int_%s *) "
978 "__gdb_stack[__gdb_tos]);\n",
984 unary (indent, stream,
985 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos]) < 0 ? "
986 "-__gdb_stack[__gdb_tos] : __gdb_stack[__gdb_tos]");
990 unary (indent, stream, "-__gdb_stack[__gdb_tos]");
994 unary (indent, stream, "~__gdb_stack[__gdb_tos]");
997 case DW_OP_plus_uconst:
998 op_ptr = safe_read_uleb128 (op_ptr, op_end, ®);
999 unary (indent, stream, "__gdb_stack[__gdb_tos] + %s",
1004 binary (indent, stream, ("((" GCC_INTPTR
1005 ") __gdb_stack[__gdb_tos-1]) / (("
1006 GCC_INTPTR ") __gdb_stack[__gdb_tos])"));
1010 binary (indent, stream,
1011 "((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) >> "
1012 "__gdb_stack[__gdb_tos]");
1015 #define BINARY(OP) \
1016 binary (indent, stream, "%s", "__gdb_stack[__gdb_tos-1] " #OP \
1017 " __gdb_stack[__gdb_tos]"); \
1040 #define COMPARE(OP) \
1041 binary (indent, stream, \
1042 "(((" GCC_INTPTR ") __gdb_stack[__gdb_tos-1]) " #OP \
1044 ") __gdb_stack[__gdb_tos]))"); \
1061 case DW_OP_call_frame_cfa:
1064 CORE_ADDR text_offset;
1066 const gdb_byte *cfa_start, *cfa_end;
1068 if (dwarf2_fetch_cfa_info (arch, pc, per_cu,
1070 &text_offset, &cfa_start, &cfa_end))
1073 pushf_register (indent, stream, registers_used, arch, regnum,
1078 /* Another expression. */
1081 /* Generate a unique-enough name, in case the CFA is
1082 computed multiple times in this expression. */
1083 xsnprintf (cfa_name, sizeof (cfa_name),
1084 "__cfa_%ld", (long) (op_ptr - base));
1086 do_compile_dwarf_expr_to_c (indent, stream,
1087 GCC_UINTPTR, cfa_name,
1088 sym, pc, arch, registers_used,
1091 &text_offset, per_cu);
1092 pushf (indent, stream, "%s", cfa_name);
1099 offset = extract_signed_integer (op_ptr, 2, byte_order);
1101 fprintfi_filtered (indent, stream, "goto ");
1102 print_label (stream, scope, op_ptr + offset - base);
1103 fprintf_filtered (stream, ";\n");
1107 offset = extract_signed_integer (op_ptr, 2, byte_order);
1109 fprintfi_filtered (indent, stream,
1110 "if ((( " GCC_INTPTR
1111 ") __gdb_stack[__gdb_tos--]) != 0) goto ");
1112 print_label (stream, scope, op_ptr + offset - base);
1113 fprintf_filtered (stream, ";\n");
1120 error (_("unhandled DWARF op: %s"), get_DW_OP_name (op));
1124 fprintfi_filtered (indent, stream, "%s = __gdb_stack[__gdb_tos];\n",
1126 fprintfi_filtered (indent - 2, stream, "}\n");
1128 do_cleanups (cleanup);
1131 /* See compile.h. */
1134 compile_dwarf_expr_to_c (struct ui_file *stream, const char *result_name,
1135 struct symbol *sym, CORE_ADDR pc,
1136 struct gdbarch *arch, unsigned char *registers_used,
1137 unsigned int addr_size,
1138 const gdb_byte *op_ptr, const gdb_byte *op_end,
1139 struct dwarf2_per_cu_data *per_cu)
1141 do_compile_dwarf_expr_to_c (2, stream, GCC_UINTPTR, result_name, sym, pc,
1142 arch, registers_used, addr_size, op_ptr, op_end,
1146 /* See compile.h. */
1149 compile_dwarf_bounds_to_c (struct ui_file *stream,
1150 const char *result_name,
1151 const struct dynamic_prop *prop,
1152 struct symbol *sym, CORE_ADDR pc,
1153 struct gdbarch *arch, unsigned char *registers_used,
1154 unsigned int addr_size,
1155 const gdb_byte *op_ptr, const gdb_byte *op_end,
1156 struct dwarf2_per_cu_data *per_cu)
1158 do_compile_dwarf_expr_to_c (2, stream, "unsigned long ", result_name,
1159 sym, pc, arch, registers_used,
1160 addr_size, op_ptr, op_end, NULL, per_cu);